Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sanowar's avatar

API CORS errors

Dear, I tried to implement your laravel api with my react project. In GET method didn't get any error but in POST method I get cors error. I tried to solve this issue but I am unable to find proper solution. Now what can I do please give me a proper solution. Thank you

0 likes
3 replies
Tray2's avatar

Give us the code and the error message and we might be abler to help you.

sanowar's avatar

@Tray2

Frontend code const handleSubmit = () => {

//  let fromData=new FormData()
//   fromData.append("name",inputData.name )
//   fromData.append("password",inputData.password.trim() )
//   fromData.append("id",userInfo.id )
   
    const data = {
        name: inputData.name,
        password: inputData.password.trim(),
        id : userInfo.id
    };

    if (inputData.name !== '' && inputData.password !== '') {
        console.log(data)
        console.log(config)
        axios
            .post(`${baseUrl}/profile/update`, data, config)
            .then((res) => {
               console.log(res.data)
                if (res.data.result) {
                    toast.success('Successfully Updated !', {
                        position: toast.POSITION.TOP_CENTER,
                    });
                } else {
                    toast.error('Server Error.. Try Again !', {
                        position: toast.POSITION.TOP_RIGHT,
                    });
                }
            })
            .catch((err) => {

                console.log(err)
                toast.error('Server Error.. Try Again !', {
                    position: toast.POSITION.TOP_RIGHT,
                });
            });
    }
};

Error message

user-information:1 Access to XMLHttpRequest at 'https://piarabazar.nabiaz.xyz/profile/update' from origin 'http://localhost:3000' 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.

Tray2's avatar

@sanowar You need to use the same origin as the requested endpoint or allow localhost on your live server (which is not a good idea),

Please or to participate in this conversation.