我正在尝试向服务器发送帖子请求,但我似乎不断收到此错误:
XMLHttpRequest cannot load https://nim-rd.nuance.mobi:9443/nina-webapi/TTS/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
这是我的javascript函数:
function play(){
var request = new XMLHttpRequest();
request.open("POST", http_url, true);
request.setRequestHeader("Content-type", "application/json");
request.setRequestHeader("nmaid", nmaid);
request.setRequestHeader("nmaidkey", nmaidKey);
request.responseType = "arraybuffer";
request.send(JSON.stringify({
text: "Hi my name is Ted",
tts_type: "text"
}));
request.onreadystatechange = function(event) {
console.log("Entered");
if (request.readyState === 4 && request.status === 200) {
console.log("Entered2");
}
}
}
以下是响应标题:
OPTIONS /nina-webapi/TTS/ HTTP/1.1
Host: nim-rd.nuance.mobi:9443
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: null
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
Access-Control-Request-Headers: content-type, nmaid, nmaidkey
Accept: */*
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
这是请求标题:
HTTP/1.1 200 OK
Allow: POST, TRACE, OPTIONS
Content-Length: 0
Server: Jetty(9.3.11.v20160721)
我在网上进行了一些研究,看起来帖子不被视为预检请求,为什么我一直收到这个错误?我也尝试将以下内容添加到我的代码中,但没有运气......
request.setRequestHeader("Access-Control-Allow-Origin", "*");
request.setRequestHeader("Access-Control-Allow-Headers", "*");
request.setRequestHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");