我正在使用骨干从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


知道为什么send方法会改变吗?

其他发现:

即使我实际上没有更改模型,它也会在执行模型时显示。

更多详细信息:这是未经授权的呼叫的请求/响应...

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

最佳答案

您似乎正在发出“ not so simple request ™”:


您正在提出CORS请求
并且您正在设置自定义标题


在这种情况下,浏览器将您的请求分为两部分:预检请求(您看到的OPTIONS动词)和检索到权限后的实际请求。

引用链接的文章:


  预检请求是作为HTTP OPTIONS请求发出的(因此请确保
  您的服务器能够响应此方法)。它还包含一些
  其他标题:
  
  Access-Control-Request-Method-实际请求的HTTP方法。
  即使HTTP方法是
  前面定义的简单HTTP方法(GET,POST,HEAD)。
  
  Access-Control-Request-Headers-用逗号分隔的非简单列表
  请求中包含的标头。
  
  预检请求是一种询问实际许可的方法
  提出实际要求之前。服务器应检查
  上面的两个标头,以验证HTTP方法和
  请求的标头有效且被接受。

关于javascript - 在主干中的 header 中添加访问 token 的问题:它更改了方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19932815/

10-12 12:58
查看更多