当我从localhost:8080启动POST请求到外部API时,我遇到了频繁的405问题。我试图使用许多不同的选项,但我没有得到问题的关键。
URL = api end point; //Your URL
var datajson = '{'
+'"image" : "'+imageBase64+'"'
+'}';
var req = new XMLHttpRequest();
var body = JSON.parse(datajson)
if ('withCredentials' in req) {
req.open('POST', URL, true);
req.setRequestHeader('Content-Type', 'application/json');
req.onreadystatechange = handleResponse(req);
req.send(body);
}
我在浏览器中遇到的错误是:
选项http://blablabla 405(方法不允许)
sendToBioFace @ myjavascript.js:38
发送@ myjavascript.js:56
onclick @(index):13
(索引):1无法加载http://blablabla:预检的响应具有无效的HTTP状态代码405
在下面找到标题:
Request URL:http://blablabla.com
Request Method:OPTIONS
Status Code:405 Method Not Allowed
Remote Address: destinationip:16111
Referrer Policy:no-referrer-when-downgrade
Response Headers
Access-Control-Allow-Headers:X-Requested-With,Content-Type
Access-Control-Allow-Origin:*
Access-Control-Request-Method:POST,GET,PUT,DELETE,OPTIONS
Allow:POST
Content-Length:1569
Content-Type:text/html; charset=UTF-8
Date:Sun, 28 Jan 2018 19:13:42 GMT
Server:Microsoft-HTTPAPI/2.0
Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:es-ES,es;q=0.9,en;q=0.8
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host: destinationip:16111
Origin:http://localhost:8080
Pragma:no-cache
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
答案 0 :(得分:-1)
您正在尝试发送跨域请求,但您发送的标头不包含OPTIONS。
您应该为传出请求添加OPTIONS请求标头。