Backbone: Sinkronisasi CORS mengirimkan permintaan OPTIONS alih-alih POST

Dibuat pada 17 Mei 2013  ·  17Komentar  ·  Sumber: jashkenas/backbone

I'm trying to use the model.save() with a rest service on another subdomain. Saya mencoba menggunakan model.save() dengan layanan istirahat di subdomain lain. I got the following request headers which are far from my expectations: Saya mendapatkan tajuk permintaan berikut yang jauh dari harapan saya:

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? Bagaimana cara memperbaikinya?

Ofc. Ofc. my REST server responds 404 not found for an OPTIONS call... server REST saya merespons 404 tidak ditemukan untuk panggilan OPSI ...

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. Untuk menghindari pertanyaan yang tidak diinginkan: server baik-baik saja, menangani panggilan REST dan panggilan CORS dengan baik, diuji dengan $.ajax dan juga aplikasi penguji antarmuka REST.

en

Komentar yang paling membantu

@inf3rno try this one. @inf3rno coba yang ini.

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. Ini sama sekali bukan masalah Backbone.

en

Semua 17 komentar

Look up how CORS works -- this is the expected "preflight" request Cari tahu cara kerja CORS -- ini adalah permintaan "preflight" yang diharapkan

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

Bagaimana saya bisa menghindari permintaan reflight ini?

en

Hanya mengirim "permintaan sederhana" seperti yang didefinisikan di sini: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS#Simple_requests

en

I tried this on server side to handle preflight calls: Saya mencoba ini di sisi server untuk menangani panggilan preflight:

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... tetapi tidak berfungsi, browser tidak mengirim permintaan lain setelah preflight...

So you say that it is not possible with backbone.sync? Jadi Anda mengatakan bahwa itu tidak mungkin dengan backbone.sync?
Then it is a bug I think... Maka itu adalah bug yang saya pikir ...

en

Terlepas dari itu, ini bukan masalah Backbone, jadi saya sarankan bertanya di Stackoverflow atau IRC.

en

Bisakah Anda menjelaskan kepada saya mengapa ini bukan masalah tulang punggung?

en

Baca melalui sumber Backbone.sync ( http://backbonejs.org/docs/backbone.html#section-134 ) dan Anda akan melihat Backbone hanya meneruskan semuanya ke $.ajax.

en

@inf3rno try this one. @inf3rno coba yang ini.

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. Ini sama sekali bukan masalah Backbone.

en

These are my original headers without php: Ini adalah tajuk asli saya tanpa 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... Saya pikir server saya dikonfigurasi dengan baik, tetapi saya mencoba tajuk yang Anda berikan, dan tidak berfungsi ...

Simple $.ajax calls work well, for example I load json files from the service with this: Panggilan $.ajax sederhana berfungsi dengan baik, misalnya saya memuat file json dari layanan dengan ini:

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... Saya mencoba ini dengan metode permintaan apa pun, dan mereka merespons dengan baik ...

The collection.fetch() works well too, I have problems only with the model.save() ... Collection.fetch() juga berfungsi dengan baik, saya hanya punya masalah dengan model.save() ...

I tried it this way, maybe I'm doing something wrong: Saya mencobanya dengan cara ini, mungkin saya melakukan sesuatu yang salah:

        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. Saya mendapatkan atribut dan "kesalahan" di konsol. I'll check what kind of error is.. nanti saya cek errornya seperti apa..

en

You were right, thanks! Anda benar, terima kasih! I added to server config the allow content-type, and it works now. Saya menambahkan ke konfigurasi server tipe konten yang diizinkan, dan itu berfungsi sekarang. Firebug is not the best, the Chrome console printed that the content-type is not allowed... Firebug bukan yang terbaik, konsol Chrome mencetak bahwa tipe konten tidak diizinkan ...

en

Many tanks! Banyak tank! :D :D

en

GET and POST requests requires quite different headers. Permintaan GET dan POST membutuhkan header yang sangat berbeda. When the API server uses cookies for authentication, the server should have Access-Control-Allow-Credentials: true header. Ketika server API menggunakan cookies untuk otentikasi, server harus memiliki header Access-Control-Allow-Credentials: true . And off course you need to make a XHR request with withCredentials: true on client-side. Dan tentu saja Anda perlu membuat permintaan XHR dengan withCredentials: true di sisi klien. When the API server uses Authorization header for BasicAuth request, you don't need withCredential . Saat server API menggunakan Authorization header untuk permintaan BasicAuth, Anda tidak memerlukan withCredential . Instead, the Access-Control-Allow-Headers need to include Authorization in this case, just like Sebaliknya, Access-Control-Allow-Headers perlu menyertakan Authorization dalam kasus ini, seperti

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

Ah, it seems you solved the issue while I'm writing this. Ah, sepertinya Anda memecahkan masalah saat saya menulis ini.

en

Ye, terima kasih, Anda banyak membantu!

en

@inf3rno Thanks so much for the comment about Chrome giving better error messages than Firebug! @inf3rno Terima kasih banyak atas komentarnya tentang Chrome yang memberikan pesan kesalahan yang lebih baik daripada Firebug! That helped us solve the problem. Itu membantu kami memecahkan masalah.

en

You're welcome! Terima kasih kembali! :-) :-)

en

Jika ada orang lain yang mengalami masalah ini, saya harus menambahkan "x-http-method-override" ke daftar Access-Control-Allow-Methods saya yang digunakan saat Anda menyetel Backbone.emulateHTTP = true

en
Apakah halaman ini membantu?
0 / 5 - 0 peringkat