'404 when trying to POST request
I'm making a web app and it has a login page. Every time I submit the login credentials I keep getting this error.
POST http://localhost:3000/api/user/login 404 (Not Found)
Everything works fine on postman.
My userActions.js
export const login = (email, password) => async (dispatch) => {
try{
dispatch({type: USER_LOGIN_REQUEST})
const config = {
header: {
"Content-Type": "application/json",
},
}
const {data} = await axios.post('/api/user/login',
{ email, password},
config)
dispatch({type: USER_LOGIN_SUCCESS, payload: data})
localStorage.setItem('userInfo', JSON.stringify(data))
} catch (error) {
dispatch({
type: USER_LOGIN_FAIL,
payload: error.response && error.response.data.message ?
error.response.data.message : error.message,
})
}
}
My react app is running on PORT 8675
POST http://localhost:8675/api/user/login in Postman works fine
POST on port 3000 in postman doesn't work however
Solution 1:[1]
You need to write true link on axios.post("...)
const baseUrl = 'http://localhost:8675/';
const {data} = await axios.post(baseUrl +'api/user/login',...}
// or
const {data} = await axios.post('http://localhost:8675/api/user/login',...}
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 |
