[英]CORS issue? - Response for preflight has invalid HTTP status code 405
I am facing the frequent 405 problem when launching a POST request from my localhost:8080 to an external API. 从本地主机:8080向外部API启动POST请求时,我经常遇到405问题。 I've tried to use many different options but I don´t get the key of the problem. 我尝试使用许多不同的选项,但是我没有找到问题的关键。
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);
}
The error I get in browser is: 我在浏览器中得到的错误是:
OPTIONS http://blablabla 405 (Method Not Allowed) 选项http:// blablabla 405(不允许使用方法)
sendToBioFace @ myjavascript.js:38 sendToBioFace @ myjavascript.js:38
send @ myjavascript.js:56 发送@ myjavascript.js:56
onclick @ (index):13 onclick @((索引):13)
(index):1 Failed to load http://blablabla : Response for preflight has invalid HTTP status code 405 (索引):1无法加载http:// blablabla :预检响应包含无效的HTTP状态代码405
Find below the Headers: 在标题下方找到:
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
You are trying to send cross domain request but the headers that you sent doesn't include OPTIONS. 您正在尝试发送跨域请求,但是您发送的标头不包含OPTIONS。
You should add OPTIONS request header to your outgoing requests. 您应该将OPTIONS请求标头添加到外发请求中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.