1

In my next.js project I'm trying to send a POST request using axios:

axios.defaults.baseURL = "https://{my-server-url}/api";

useEffect(() => {
        axios({
            method: "POST",
            url: "/build_pages",
            data: {
                page_id: 3,
            },
        }).then((res) => {
            console.log(res.data);
        });
    }, []);

I'm getting the following CORS error: enter image description here

My server is [laravel] php and is deployed on cpanel, but before I deployed my backend to cpanel (i.e. when I was working on localhost) I did not get this error.

How do I turn off this error (note that I don't want to download any web browser extension to fix the CORS error)

I came across this link but it does not say where (which file) to add it to so I'm stuck

Edit: I tried the middleware solution provided in this answer: https://stackoverflow.com/a/69151121/12009071 but also it didn't work, howerver the error changed to:

Access to XMLHttpRequest at 'https://{my-server-url}/api/check_admin_login' from origin 'https://{my-server-url}.vercel.app' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
Daniel_Kamel
  • 610
  • 8
  • 29

1 Answers1

1

This is an API-side error. You have to allow cross origin requests on the API server. Based on your question, I assume your API is NOT the Next.js project. Therefore, you should check the settings of your server. The next.js POST call is not the issue.

ajpa
  • 21
  • 2
  • yes i know the problem is from the server side, my server is [laravel] php, but what do i do and change to make sure i dont get the error anymore? – Daniel_Kamel Mar 18 '22 at 11:48
  • 2
    You need to ask another question regarding laravel and php. On the question you should give information such as the laravel version you are using, how the CORS configuration curretly stands, and the solutions that you have tried. https://stackoverflow.com/search?q=cors+laravel – ajpa Mar 18 '22 at 11:59