将访问令牌添加到主干中的标头时出现问题:它会更改方法
Issue adding access token to header in Backbone: It changes the method
我正在使用骨干网从API获取数据。 当不需要授权并且当我将授权添加到 API 时,我得到预期的 401 - 未经授权的响应时,这一切都可以正常工作。
[从控制台日志:
GET http://localhost:999/api/tasks 401 (Unauthorized)
]
然后,我添加了以下代码,以将持有者授权添加到每个调用的标头中:
var backboneSync = Backbone.sync;
Backbone.sync = function (method, model, options) {
/*
* The jQuery `ajax` method includes a 'headers' option
* which lets you set any headers you like
*/
var theUser = JSON.parse(localStorage.getItem("happuser"));
if (theUser !== null)
{
var new_options = _.extend({
beforeSend: function(xhr) {
var token = 'Bearer' + theUser.authtoken;
console.log('token', token);
if (token) xhr.setRequestHeader('Authorization', token);
}
}, options)
}
/*
* Call the stored original Backbone.sync method with
* extra headers argument added
*/
backboneSync(method, model, new_options);
};
一旦我包含此代码,API 就会使用 OPTIONS 而不是 GET 的方法发送请求,我显然得到一个 405 无效的方法响应。
这是控制台日志输出
OPTIONS http://localhost:999/api/tasks 405 (Method Not Allowed) jquery-1.7.2.min.js:4
OPTIONS http://localhost:999/api/tasks Invalid HTTP status code 405
知道为什么发送方法会改变吗?
其他发现:
当我做一个模型保存时,它就会做同样的事情,即使我实际上没有改变模型。
更多详细信息:这是未经授权呼叫的请求/响应...
Request URL:http://localhost:999/api/tasks
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Host:localhost:999
Origin:http://localhost
Proxy-Connection:keep-alive
Referer:http://localhost/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Response Headersview source
Access-Control-Allow-Headers:*
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:http://localhost
Cache-Control:no-cache
Content-Length:3265
Content-Type:application/json; charset=utf-8
Date:Wed, 13 Nov 2013 14:51:32 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
一旦我在响应中添加同步覆盖代码,就会更改为:
Request URL:http://localhost:999/api/tasks
Request Method:OPTIONS
Status Code:405 Method Not Allowed
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Access-Control-Request-Headers:accept, authorization
Access-Control-Request-Method:GET
Host:localhost:999
Origin:http://localhost
Proxy-Connection:keep-alive
Referer:http://localhost/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Response Headersview source
Access-Control-Allow-Headers:*
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:http://localhost
Allow:GET,POST
Cache-Control:no-cache
Content-Length:76
Content-Type:application/json; charset=utf-8
Date:Wed, 13 Nov 2013 14:56:52 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
您似乎正在发出"不那么简单的请求™":
- 您正在发出 CORS 请求
- 并且您正在设置自定义标头
在这种情况下,您的浏览器将您的请求一分为二:预检请求(您看到的 OPTIONS 动词)和检索权限后的实际请求。
引用链接的文章:
预检请求作为 HTTP 选项请求发出(因此请确保 您的服务器能够响应此方法)。它还包含一些 其他标头:
访问控制请求方法 - 实际请求的 HTTP 方法。 始终包含此请求标头,即使 HTTP 方法是 前面定义的简单HTTP方法(GET,POST,HEAD)。
访问控制请求标头 - 逗号分隔的非简单列表 请求中包含的标头。
预检请求是一种请求实际权限的方法 请求,然后再提出实际请求。服务器应检查 上面的两个标头来验证 HTTP 方法和 请求的标头有效且被接受。
相关文章:
- 文档.编写方法问题
- 使用Ajax的问题's发送多个值的Post方法
- 在对象中添加方法时出现问题
- 简单的javascript方法问题
- Node.js-异步方法调用问题
- 尝试将变量传递到对象中时的作用域问题'的方法构造函数
- 当服务器从 http 更改为 https 时,有哪些可能的方法可以解决问题
- .attr(“href”) 的问题!解决此问题的简单方法
- 将方法分配给变量和问题
- 随机数生成器,what'我的方法/统计数据有问题吗?[JS]
- 有没有一种方法可以在IE8中解决我的Rails javascript应用程序.js的问题
- 对象中的setInterval出现问题's方法
- 包含方法和突出显示元素的问题
- 谷歌地图API v3 addDomListener方法问题
- 这种计算画布元素中非白色像素的方法有什么问题
- Javascript替换方法有什么问题
- 角度非幂等方法问题
- 将访问令牌添加到主干中的标头时出现问题:它会更改方法
- Android驱动程序的scrollTo和scrollToExact 方法的问题.方法不会滚动到所需的元素文本,而是滚动
- 在集合中引用变量时出现问题.方法在渲染函数中