Backbone: CORS同期はPOSTの代わりにOPTIONSリクエストを送信します

作成日 2013年05月17日  ·  17コメント  ·  ソース: jashkenas/backbone

I'm trying to use the model.save() with a rest service on another subdomain.別のサブドメインのRESTサービスでmodel.save()を使用しようとしています。 I got the following request headers which are far from my expectations:次のリクエストヘッダーを取得しましたが、これは私の期待からはほど遠いものです。

OPTIONS /user HTTP/1.1
Host: not.public
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://not.public
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

How to fix this?これを修正する方法は?

Ofc.多くの場合。 my REST server responds 404 not found for an OPTIONS call...私のRESTサーバーは、OPTIONS呼び出しに対して404が見つかりませんでした。

To avoid unwanted questions: the server is ok, it handles REST calls and CORS calls well, tested with $.ajax and a REST interface tester application too.不要な質問を避けるために、サーバーは問題なく、REST呼び出しとCORS呼び出しを適切に処理し、$。ajaxとRESTインターフェーステスターアプリケーションでもテストされています。

en

最も参考になるコメント

@inf3rno try this one. @inf3rnoこれを試してください。

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: POST');

This isn't Backbone issue at all.これはバックボーンの問題ではありません。

en

全てのコメント17件

Look up how CORS works -- this is the expected "preflight" request CORSがどのように機能するかを調べます-これは予想される「プリフライト」リクエストです

https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Preflighted_requests https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Preflighted_requests

en

このリフライトリクエストを回避するにはどうすればよいですか?

en

ここで定義されている「単純なリクエスト」のみを送信します: https ://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Simple_requests

en

I tried this on server side to handle preflight calls:プリフライトコールを処理するためにサーバー側でこれを試しました:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');

but did not work, the browser does not send another request after the preflight...しかし、動作しませんでした、ブラウザはプリフライト後に別のリクエストを送信しません...

So you say that it is not possible with backbone.sync?それで、backbone.syncでは不可能だと言いますか?
Then it is a bug I think...それからそれは私が思うバグです...

en

とにかく、これはバックボーンの問題ではないので、StackoverflowまたはIRCで質問することをお勧めします。

en

これがバックボーンの問題ではない理由を説明できますか?

en

Backbone.syncのソース(http://backbonejs.org/docs/backbone.html#section-134)を読むと、Backboneがすべてを$.ajaxに渡すだけであることがわかります。

en

@inf3rno try this one. @inf3rnoこれを試してください。

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: POST');

This isn't Backbone issue at all.これはバックボーンの問題ではありません。

en

These are my original headers without php:これらは、PHPを使用しない元のヘッダーです。

HTTP/1.1 200 OK
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: text/html
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.3.8, ASP.NET
access-control-allow-origin: http://x.y.loc
Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
Date: Fri, 17 May 2013 01:33:02 GMT
Content-Length: 0

I think my server is well configured, but I tried out the headers you gave, and they did not work...私のサーバーは適切に構成されていると思いますが、あなたが提供したヘッダーを試してみましたが、機能しませんでした...

Simple $.ajax calls work well, for example I load json files from the service with this:単純な$.ajax呼び出しはうまく機能します。たとえば、次のようにサービスからjsonファイルをロードします。

define(function (require, exports, module) {
    var $ = require("jquery");

    module.exports = {
        load: function (name, _require, load, config) {
            var params = name.split("|");
            var method, url;
            if (params.length == 1) {
                method = "GET";
                url = params[0];
            }
            else {
                method = params[0];
                url = params[1];
            }
            $.ajax({
                url: url,
                type: method,
                dataType: "json"
            }).success(load).error(function (xhr, status, error) {
                    throw error;
                });
        }
    };

});

I tried out this with any request method, and they responded well...私はこれをどんなリクエスト方法でも試してみました、そして彼らはうまく反応しました...

The collection.fetch() works well too, I have problems only with the model.save() ... collection.fetch()もうまく機能しますが、model.save()でのみ問題が発生します...

I tried it this way, maybe I'm doing something wrong:私はそれをこのように試しました、多分私は何か間違ったことをしています:

        var User = Backbone.Model.extend({
            urlRoot: Application.service.Authorization.User.create.url
        });
        var form = new UserForm({
            model: new User({
                email: "[email protected]",
                name: "test"
            })
        }).render();
        form.on("submit", function (user) {
            console.log(user.attributes);
            user.save(null, {
                success: function (model, response, options) {
                    console.log("success");
                },
                error: function (model, xhr, options) {
                    console.log("error");
                }
            });
        });

I got the attributes and "error" in console.コンソールに属性と「エラー」が表示されました。 I'll check what kind of error is..エラーの種類を確認します。

en

You were right, thanks!あなたは正しかった、ありがとう! I added to server config the allow content-type, and it works now.サーバー構成にallowcontent-typeを追加しましたが、これで機能します。 Firebug is not the best, the Chrome console printed that the content-type is not allowed... Firebugは最高ではありません、Chromeコンソールはコンテンツタイプが許可されていないことを印刷しました...

en

Many tanks!多くの戦車! :D :D

en

GET and POST requests requires quite different headers. GETPOSTのリクエストには、まったく異なるヘッダーが必要です。 When the API server uses cookies for authentication, the server should have Access-Control-Allow-Credentials: true header. APIサーバーが認証にcookiesを使用する場合、サーバーにはAccess-Control-Allow-Credentials: trueヘッダーが必要です。 And off course you need to make a XHR request with withCredentials: true on client-side.そしてもちろん、クライアント側でwithCredentials: trueを使用してXHRリクエストを行う必要があります。 When the API server uses Authorization header for BasicAuth request, you don't need withCredential . APIサーバーがBasicAuthリクエストにAuthorizationヘッダーを使用する場合、 withCredentialは必要ありません。 Instead, the Access-Control-Allow-Headers need to include Authorization in this case, just like代わりに、この場合、 Access-Control-Allow-HeadersにはAuthorizationを含める必要があります。

header('Access-Control-Allow-Headers: Authorization, Content-Type');

Ah, it seems you solved the issue while I'm writing this.ああ、私がこれを書いている間にあなたは問題を解決したようです。

en

ええ、とにかくありがとう、あなたはたくさん助けました!

en

@inf3rno Thanks so much for the comment about Chrome giving better error messages than Firebug! @ inf3rnoChromeがFirebugよりも優れたエラーメッセージを表示することについてコメントしてくれてありがとう! That helped us solve the problem.それは私たちが問題を解決するのに役立ちました。

en

You're welcome!どういたしまして! :-) :-)

en

他の誰かがこの問題を抱えている場合は、Backbone.emulateHTTP=trueを設定したときに使用されるAccess-Control-Allow-Methodsのリストに「x-http-method-override」を追加する必要がありました。

en
このページは役に立ちましたか?
0 / 5 - 0 評価