I was consuming an api using XHR, but the same request does not work using axios.
With XHR:
var data = JSON.stringify(false);
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("POST", "api/url");
xhr.send(data);
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
With Axios:
axios({
method: 'post',
url: 'api/url',
data: JSON.stringify(false),
withCredentials: true
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Error is
"Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."
Response Headers on XHR
- HTTP/1.1 200 OK
- Accept-Ranges: bytes
- Accept-Ranges: bytes
- Access-Control-Allow-Credentials: true
- Access-Control-Allow-Origin:http://localhost:8000
- Age: 0
- Cache-Control: no-cache, no-store, max-age=0, must-revalidate
- Content-Type: application/json;charset=UTF-8
- Date: Tue, 26 Dec 2017 13:50:01 GMT
- Expires: 0
- Pragma: no-cache
- Vary: Origin
Response Headers on Axios:
- HTTP/1.1 404 Not Found
- Accept-Ranges: bytes
- Age: 0
- Content-Type: application/json; charset=utf-8
- Date: Tue, 26 Dec 2017 14:01:26 GMT