2

バックボーンを使用して 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 は GET ではなく OPTIONS のメソッドを使用してリクエストを送信し、明らかに 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
4

1 に答える 1