'axios post / get always return 404 in react
// Login Method
const postLogin = (url, data) => {
return axios.post(url, data).then(response => {
if (response.status === 400 || response.status === 500)
throw response.data;
console.log(response);
return response;
}).catch(err => {
console.log(err);
throw err[1];
});
}
The above login method always log the following error:
Error: Request failed with status code 404
at createAxiosError (utils.js:147:1)
at Object.settle (utils.js:123:1)
at handleRequest (handle_request.js:124:1)
at index.js:26:1
at new Promise (<anonymous>)
at MockAdapter.<anonymous> (index.js:25:1)
at dispatchRequest (dispatchRequest.js:52:1)
On call
const response = yield call(postLogin, 'http://localhost:8080/users/verify_login/', {
email: user.username,
password: user.password,
headers: {
"Content-Type": "application/json",
"accept" : "application/json",
}});
Though the http://localhost:8080 is running. We are using React. How to solve this?
Solution 1:[1]
You can try using async await for better debugging your code.
const postLogin = async (url, data) => {
try{
var response = await axios.post(url, data)
return response
}catch(e){
// Handle catch
}finally{
// Handle finally
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JaivBhup |
