Backbone: Die CORS-Synchronisierung sendet eine OPTIONS-Anforderung anstelle von POST

Erstellt am 17. Mai 2013  ·  17Kommentare  ·  Quelle: jashkenas/backbone

I'm trying to use the model.save() with a rest service on another subdomain. Ich versuche, model.save() mit einem Rest-Service auf einer anderen Subdomain zu verwenden. I got the following request headers which are far from my expectations: Ich habe die folgenden Anforderungsheader erhalten, die weit von meinen Erwartungen entfernt sind:

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? Wie kann man das beheben?

Ofc. Ofc. my REST server responds 404 not found for an OPTIONS call... mein REST-Server antwortet 404 nicht gefunden für einen OPTIONS-Aufruf ...

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. Um unerwünschte Fragen zu vermeiden: Der Server ist in Ordnung, er verarbeitet REST-Aufrufe und CORS-Aufrufe gut, getestet mit $.ajax und einer REST-Schnittstellentestanwendung.

en

Hilfreichster Kommentar

@inf3rno try this one. @inf3rno versuchen Sie es hier.

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. Dies ist überhaupt kein Backbone-Problem.

en

Alle 17 Kommentare

Look up how CORS works -- this is the expected "preflight" request Sehen Sie nach, wie CORS funktioniert – dies ist die erwartete „Preflight“-Anfrage

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

Wie kann ich diese Rückfluganfrage vermeiden?

en

Senden Sie nur eine "einfache Anfrage", wie hier definiert: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Simple_requests

en

I tried this on server side to handle preflight calls: Ich habe dies auf der Serverseite versucht, um Preflight-Aufrufe zu verarbeiten:

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... hat aber nicht funktioniert, der Browser sendet nach dem Preflight keine weitere Anfrage...

So you say that it is not possible with backbone.sync? Sie sagen also, dass es mit backbone.sync nicht möglich ist?
Then it is a bug I think... Dann ist es ein Bug denke ich...

en

Unabhängig davon ist dies kein Backbone-Problem, daher schlage ich vor, bei Stackoverflow oder IRC nachzufragen.

en

Können Sie mir erklären, warum dies kein Backbone-Problem ist?

en

Lesen Sie die Quelle von Backbone.sync ( http://backbonejs.org/docs/backbone.html#section-134 ) und Sie werden sehen, dass Backbone einfach alles an $.ajax weiterleitet.

en

@inf3rno try this one. @inf3rno versuchen Sie es hier.

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. Dies ist überhaupt kein Backbone-Problem.

en

These are my original headers without php: Dies sind meine ursprünglichen Header ohne 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... Ich denke, mein Server ist gut konfiguriert, aber ich habe die von Ihnen angegebenen Header ausprobiert und sie haben nicht funktioniert ...

Simple $.ajax calls work well, for example I load json files from the service with this: Einfache $.ajax-Aufrufe funktionieren gut, zum Beispiel lade ich hiermit json-Dateien aus dem Dienst:

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... Ich habe das mit jeder Anfragemethode ausprobiert, und sie haben gut reagiert ...

The collection.fetch() works well too, I have problems only with the model.save() ... Die collection.fetch() funktioniert auch gut, ich habe nur Probleme mit der model.save() ...

I tried it this way, maybe I'm doing something wrong: Ich habe es so versucht, vielleicht mache ich etwas falsch:

        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. Ich habe die Attribute und "Fehler" in der Konsole erhalten. I'll check what kind of error is.. Ich schau mal was das für ein Fehler ist..

en

You were right, thanks! Du hattest Recht, danke! I added to server config the allow content-type, and it works now. Ich habe der Serverkonfiguration den Allow-Content-Type hinzugefügt, und es funktioniert jetzt. Firebug is not the best, the Chrome console printed that the content-type is not allowed... Firebug ist nicht der Beste, die Chrome-Konsole hat ausgegeben, dass der Inhaltstyp nicht zulässig ist ...

en

Many tanks! Viele Panzer! :D :D

en

GET and POST requests requires quite different headers. GET - und POST -Anforderungen erfordern ganz unterschiedliche Header. When the API server uses cookies for authentication, the server should have Access-Control-Allow-Credentials: true header. Wenn der API-Server cookies zur Authentifizierung verwendet, sollte der Server den Header Access-Control-Allow-Credentials: true haben. And off course you need to make a XHR request with withCredentials: true on client-side. Und natürlich müssen Sie auf Client-Seite eine XHR-Anfrage mit withCredentials: true stellen. When the API server uses Authorization header for BasicAuth request, you don't need withCredential . Wenn der API-Server den Header Authorization für die BasicAuth-Anforderung verwendet, benötigen Sie withCredential nicht. Instead, the Access-Control-Allow-Headers need to include Authorization in this case, just like Stattdessen müssen die Access-Control-Allow-Headers Authorization enthalten, genau wie

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

Ah, it seems you solved the issue while I'm writing this. Ah, anscheinend hast du das Problem gelöst, während ich dies schreibe.

en

Ja, danke trotzdem, du hast mir sehr geholfen!

en

@inf3rno Thanks so much for the comment about Chrome giving better error messages than Firebug! @inf3rno Vielen Dank für den Kommentar zu Chrome, der bessere Fehlermeldungen liefert als Firebug! That helped us solve the problem. Das hat uns geholfen, das Problem zu lösen.

en

You're welcome! Gern geschehen! :-) :-)

en

Falls jemand anderes dieses Problem hat, musste ich "x-http-method-override" zu meiner Liste der Access-Control-Allow-Methods hinzufügen, die verwendet wird, wenn Sie Backbone.emulateHTTP = true setzen

en
War diese Seite hilfreich?
0 / 5 - 0 Bewertungen