我正在尝试使用XMLHttpRequest()发出CORS请求。我在请求期间也在标题中进行了一些更改。服务器正在发送200 OK响应,但我在浏览器控制台中看到了预检响应错误:
客户端请求代码:
{
"search_metadata" = {
"completed_in" = "0.05";
count = 15;
"max_id" = 680240431771156480;
"max_id_str" = 680240431771156480;
"next_results" = "?max_id=680240407322689535&q=%23ChristmasEve&include_entities=1";
query = "%23ChristmasEve";
"refresh_url" = "?since_id=680240431771156480&q=%23ChristmasEve&include_entities=1";
"since_id" = 0;
"since_id_str" = 0;
};
statuses = (
{
contributors = "<null>";
coordinates = "<null>";
"created_at" = "Fri Dec 25 04:15:31 +0000 2015";
entities = {
hashtags = (
{
indices = (
0,
13
);
text = ChristmasEve;
},
{
服务器端响应:
var method = "POST";
var tempURL = "http://127.0.0.1:8000/myFunc/";
var body = {"key": "value"};
var xhr = new XMLHttpRequest();
xhr.open(method, tempURL, true);
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
xhr.setRequestHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
xhr.send(body);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
alert("Data: " + xhr.responseText);
};
xhr.onerror = function() {
alert('Oops, there was an error making the request.');
};
错误(在浏览器控制台中):
[25/Dec/2015 05:18:43] "OPTIONS /myFunc/ HTTP/1.1" 200 0
有人可以建议导致此问题的原因吗?需要做哪些更改以及在哪里(客户端/服务器端)?
谢谢,