为什么CORS似乎没有工作,POST?
-
23-09-2019 - |
题
Mozilla的自己的规范说简单GET
或POST
应该是本地CORS的无预检但到目前为止,每POST
尝试我做导致的OPTIONS
头走出去。当我改变它从POST
获得代码立即发送一个适当的请求GET
所以跨站部工作正常。
下面是我在做什么在Firefox中精简下来的样品:
var destinationUrl = 'http://imaginarydevelopment.com/postURL';
var invocation = new XMLHttpRequest();
if (invocation) {
invocation.open('POST', destinationUrl, true);
//tried with and without this line
//invocation.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
invocation.onreadystatechange = (function Handler() {
if (invocation.readyState == 4)
alert('Request made');
});
invocation.send(/* tried with and without data*/);
}
下面就是我已经在Chrome和IE工作:
var destinationUrl = 'http://imaginarydevelopment.com/postURL';
var destination = { url: destinationUrl, type: 'POST', success: AjaxSuccess, error: AjaxError,
dataType: 'text', contentType: 'application/x-www-form-urlencoded'
};
destination.data = { 'rows': rowList, 'token': token };
$jq.ajax(destination);
解决方案 2
好了,我不知道是什么都CONTENTTYPES实际工作,但text/plain
确实在所有3个浏览器:
var destination = { url: destinationUrl, type: 'POST', success: AjaxSuccess, error: AjaxError,
contentType: 'text/plain'
};
var postData={ 'anArray': theArray, 'token': token };
destination.data=JSON.stringify(postData);
$jq.ajax(destination);
不过到目前为止,我还没有想出什么阻止做任何事情,除了运行,即使返回了505码的成功方法的要求。添加Access-Control-Allow-Origin: *
的响应报头解决不希望读出的返回数据的浏览器。
其他提示
我有同样的问题
https://developer.mozilla.org/En/HTTP_Access_Control
说,加密类型必须是text / plain的,或者你需要使用FX4 + 所有访问标头被正确地设置
不隶属于 StackOverflow