I'm trying to use the model.save() with a rest service on another subdomain. أحاول استخدام 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. OFC. my REST server responds 404 not found for an OPTIONS call... يستجيب خادم REST الخاص بي إلى 404 غير موجود لاستدعاء OPTIONS ...
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 أيضًا.
enLook 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
enI 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... ثم إنه خطأ على ما أعتقد ...
بغض النظر ، هذه ليست مشكلة العمود الفقري ، لذلك أقترح السؤال عن Stackoverflow أو IRC.
enهل يمكن أن تشرح لي لماذا هذه ليست مشكلة العمود الفقري؟
enاقرأ مصدر Backbone.sync (http://backbonejs.org/docs/backbone.html#section-134) وسترى أن العمود الفقري يمرر كل شيء ببساطة إلى $ .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. هذه ليست مشكلة العمود الفقري على الإطلاق.
enThese 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.. سوف أتحقق من نوع الخطأ ..
enYou were right, thanks! كنت على حق شكرا! I added to server config the allow content-type, and it works now. أضفت إلى تكوين الخادم السماح بنوع المحتوى ، وهو يعمل الآن. Firebug is not the best, the Chrome console printed that the content-type is not allowed... Firebug ليس الأفضل ، فقد طبعت وحدة تحكم Chrome أن نوع المحتوى غير مسموح به ...
enMany tanks! العديد من الدبابات! :D :د
enGET
and POST
requests requires quite different headers. تتطلب الطلبات GET
و POST
رؤوس مختلفة تمامًا. 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. وبعيدًا عن الدورة التدريبية ، تحتاج إلى تقديم طلب XHR withCredentials: true
من جانب العميل. When the API server uses Authorization
header for BasicAuth request, you don't need withCredential
. عندما يستخدم خادم API رأس Authorization
لطلب BasicAuth ، فلن تحتاج إلى 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! @ inf3rno شكرًا جزيلاً على التعليق حول إعطاء Chrome رسائل خطأ أفضل من Firebug! That helped us solve the problem. ساعدنا ذلك في حل المشكلة.
enYou're welcome! على الرحب والسعة! :-) :-)
enCan someone please help on this issue? هل يمكن لأحد أن يساعد في هذه القضية؟ http://stackoverflow.com/questions/32725249/cors-filter-issue-for-authentrypoint http://stackoverflow.com/questions/32725249/cors-filter-issue-for-authentrypoint
enفي حالة تعرض أي شخص آخر لهذه المشكلة ، كان علي إضافة "x-http-method-override" إلى قائمة طرق التحكم في الوصول والسماح بالوصول والتي يتم استخدامها عند تعيين Backbone.emulateHTTP = صحيح
en
التعليق الأكثر فائدة
@inf3rno try this one. @ inf3rno جرب هذا.
This isn't Backbone issue at all. هذه ليست مشكلة العمود الفقري على الإطلاق.
en