【发布时间】:2013-08-29 00:20:27
【问题描述】:
我有这个 MooTools 代码:
new Request.JSON({
method: 'POST',
url: URL, /*URL TO ANOTHER DOMAIN*/
onSuccess: function(r){
callback(r);
}
}).post(data);
并且此代码不发送 POST 请求(仅限 OPTIONS)... 看看下面的代码(效果很好):
var http = null,
params = Object.toQueryString(data);
try {
http = new XMLHttpRequest();
} catch (e) {
try {
http = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
http = null;
alert("Your browser does not support AJAX!");
}
}
}
var url = URL;
http.onreadystatechange = function () {
if (http.readyState == 4 && http.status == 200) {
var jsonData = JSON.parse(http.responseText); /*OR EVAL*/
callback(jsonData);
}
};
http.open("POST", url);
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.send(params);
编辑:
已尝试:.setHeader('Content-Type','application/x-www-form-urlencoded');
还是什么都没有……哪里有问题?
谢谢!
【问题讨论】:
-
如果它发送一个 OPTIONS 请求,那听起来像 CORS preflight request,这表明您在跨站点请求中使用了非简单请求标头。您的非 Mootools 代码集
Content-type: application/x-www-form-urlencoded,但您的 Mootools 代码可能没有。 -
尝试在您的 mootools 请求中设置选项
headers。 -
试过:.setHeader('Content-Type','application/x-www-form-urlencoded');还是什么都没有……哪里有问题?
标签: javascript xmlhttprequest request cross-domain mootools