I am facing the frequent 405 problem when launching a POST request from my localhost:8080 to an external API. 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)
sendToBioFace @ myjavascript.js:38
send @ myjavascript.js:56
onclick @ (index):13
(index):1 Failed to load http://blablabla: Response for preflight has invalid HTTP status code 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
blablablaside. Contact their support team and check their documentation to ensure you're following it right.Access-Control-Allow-Headers,Access-Control-Allow-Origin,Access-Control-Request-Method. The browser will then check your CORS request against these values and only send it if the values and your request match. Obviously, the remote API does not allow OPTIONS requests, so you cannot call their JSON endpoints from your domains. Without knowing which remote API you are using, we cannot give you any more advice than that.