لماذا يتم تسليط الضوء على طلبي عبر المجال المسبق مع طلب خيارات؟
سؤال
وفقا لمركز مطور موزيلا HTTP Control Access Control مقال ، يمكن أن تكون طلبات النشر عبر المواقع "بسيطة"-أي لا تتطلب أي وقت مضى-إذا كان نوع محتوى الطلب هو application/x-www-form-urlencoded
.
أنا لا أحصل على هذا السلوك في Firefox ، وأنا لا أفهم على الإطلاق سبب وجود هذا. هذا هو رمز الإعداد الخاص بي:
function makeXDomainRequest(url, method, data) {
var req =
typeof XDomainRequest !== "undefined" ?
new XDomainRequest() : new XMLHttpRequest();
req.open(method || "GET", url, true);
if (typeof req.onload !== "undefined") {
req.onload = onResponseLoad;
req.onerror = onRequestError;
} else {
req.onreadystatechange = onRequestStateChange;
}
if (data && typeof req.setRequestHeader === "function") {
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
} else {
// no way to set Content-Type req header in IE's XDomainRequest:
// http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
}
req.send(data || null);
}
function onResponseLoad() {
alert("Response!\n" + this.responseText);
}
function onRequestError(args) {
alert("Error!");
}
function onRequestStateChange() {
if (this.readyState === 4) {
if (this.status === 200) {
onResponseLoad.apply(this);
} else {
onRequestError.apply(this);
}
}
}
وهنا الخادم الذي أقوم به:
// thanks to http://saltybeagle.com/cors/ for having this demo endpoint:
var URL = "http://ucommbieber.unl.edu/CORS/cors.php";
الآن إذا قمت بطلب نشر بسيط - مع إرسال البيانات باسم application/x-www-form-urlencoded
في الكود أعلاه - يتم تسليط الضوء على الطلب في Firefox مع طلب خيارات. لا يتم تسليط الضوء على الكروم. افتح Fiddler قبل تشغيل هذا لترى بنفسك:
makeXDomainRequest(URL, "POST", "name=foobar");
// alerts "Response! Hello CORS [...] You sent a POST request. Your name is foobar"
فيما يلي طلب خيارات Preflight في Fiddler (لاحظ Access-Control-Request-Method: POST
رأس على الرغم من أنني حددت نوعًا من المحتوى الآمن من نوعه ولا توجد رؤوس مخصصة):
OPTIONS http://ucommbieber.unl.edu/CORS/cors.php HTTP/1.1
Host: ucommbieber.unl.edu
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
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
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost
Access-Control-Request-Method: POST
ماذا يحدث هنا؟ هل هذا خطأ في Firefox ، أم أفعل شيئًا خاطئًا؟ شكرًا!
المحلول
لقد تحولت بالفعل إلى علة Firefox. انتهى الأمر بالحصول على FF4B6: https://bugzilla.mozilla.org/show_bug.cgi؟id=588920