3

In my Mern Stack Project I am facing a problem when I am creating a Lesson from postman its created successfully but when I am trying from my browser its given me 500 error in network tab. But in console i got CORS error and also 500 error. I am including SS in bellow if anyone can face this kind of problem please help me. I am trying all the similiar solution from stackoverflow.

Access to XMLHttpRequest at 'https://lms-api-v1.coderslab.com.bd/api/v1/lesson/add' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

const apiClient = axios.create({
  baseURL: "https://my-link",
  withCredentials: false,
  accesscontrolalloworigin: "*",
  accesscontrolallowMethods: "GET, POST, PUT, DELETE, PATCH, OPTIONS",
});

// Create Lesson
export const createLesson = (lessonData, token) => async (dispatch) => {
  try {
    dispatch({ type: NEW_LESSON_REQUEST });

    const config = {
      headers: {
        Authorization: `Bearer ${token}`,
        'Access-Control-Allow-Origin' : '*',
        'Access-Control-Allow-Credentials':true,
        'Access-Control-Allow-Methods':'GET,PUT,POST,DELETE,PATCH,OPTIONS',
      },
    };

    const { data } = await apiClient.post(`lesson/add`, lessonData, config);

    dispatch({
      type: NEW_LESSON_SUCCESS,
      payload: data,
    });
  } catch (error) {
    dispatch({
      type: NEW_LESSON_FAIL,
      payload: error.response,
    });
  }
};
md abrar
  • 27
  • 1
  • 3

2 Answers2

1

you need to allow origin from backend. like this

Access-Control-Allow-Origin: http://localhost:3000
Anik Biswas
  • 120
  • 1
  • 1
  • 7
  • but what to do when its publish in server and backend is developed by other he sayed to me that he added Access-Control-Allow-Origin in his code. – md abrar Mar 23 '22 at 14:43
  • and its also working from postman – md abrar Mar 23 '22 at 14:51
  • 3
    2022 and still this crap resides in react only so tired of it, tested same calls in laravel and django no issue seems react just doesn't work on local test boxes without creating bloated proxies – Psymon25 Aug 23 '22 at 12:22
1

Add this in backend

Access-Control-Allow-Origin: '*'
Zul Marij
  • 11
  • 2