The object error.response for axios error handling is empty
_this.$http.post(`/company/${id}/out`, {
access_token: _this.access_token,
}).then((res) => {
let info = res;
}).catch((err) => {
console.log(err.response);
// err.response undefined
})
},
I want to deal with this error at 403 or 401, but I can"t get the status code. But that"s how it works on the official website.
axios.get("/user/12345")
.catch(function (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error", error.message);
}
console.log(error.config);
});
the version of my axios is ^ 0.16.1.
notice the word "response to preflight" in it. Please note that when using post, the browser sends a "OPTIONS" request first.
this error may not have been processed by your server for "OPTIONS" requests.
post my code:
//
router.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:8080')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept,withCredentials')
res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
res.header("Access-Control-Max-Age", "1728000")
//,session
res.header('Access-Control-Allow-Credentials', 'true')
// res.header('Content-Type', 'application/json;charset=utf-8')
console.log('SESSION Name', req.session.name)
if (req.method == 'OPTIONS') {
res.end('OK')
}
else {
next()
}
})
403 is not clear, but I remember that the answer I checked before is that 401will be intercepted by the browser, and js can't get
my test doesn't get 405401, and so on. I'm a little depressed
.
you can actually get it. The problem here is because the backend has reported a cross-domain problem , which needs to be solved by the backend
.
I set up a local server. At first, I didn't set up cross-domain. As a result, like the landlord, I couldn't get response
response
.