Mas*_*low 7 w3c xmlhttprequest cors
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*/);
}
Run Code Online (Sandbox Code Playgroud)
这是我已经在 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);
Run Code Online (Sandbox Code Playgroud)
好吧,我不知道所有 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);
Run Code Online (Sandbox Code Playgroud)
然而到目前为止,即使返回 505 代码,我还没有弄清楚除了运行 success 方法之外,是什么阻止了请求执行任何操作。添加响应头Access-Control-Allow-Origin: *解决了浏览器不想读取返回数据的问题。
| 归档时间: |
|
| 查看次数: |
6842 次 |
| 最近记录: |