I am trying to test an api call being made from reactjs to a node rest layer using axios. I am receiving the following CORS error when I look at the console message from Chrome. I see the following error "Access to XMLHttpRequest at 'http://localhost:5001/api/login' from origin 'http://localhost:5000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.". The weird thing is that the code in the catch block is getting called for both methods (the one in the LoginApi is for testing purposes) but the error variable in the catch parameter keeps giving a message saying "error is not defined" when I mouseover it. 1) How is that possible for error to be undefined, if I am in the catch block because an error occurred? 2) How am I getting CORS errors if I am running from localhost and how can I resolve for testing purposes?
Here is the method in my saga file:
function* login(params){
try {
const api = new LoginApi();
const response = yield call(api.login, params);
} catch (error) {
debugger;
yield put({ type: types.ERROR_OCCURRED, error });
}
}
Here is the method in LoginApi object that is getting called from yield call:
login = async (loginProps) => {
try {
let api = axios.create({
baseURL: 'http://localhost/5001',
timeout: 100000
});
return await api.post(`/api/login`, loginProps);
} catch (error) {
debugger;
throw error;
}
}