将访问令牌添加到主干中的标头时出现问题:它会更改方法

Issue adding access token to header in Backbone: It changes the method

本文关键字:问题 方法 添加 访问令牌      更新时间:2023-09-26

我正在使用骨干网从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 方法和 请求的标头有效且被接受。