close

[Solved] No ‘Access-Control-Allow-Origin’ header is present on the requested resource—when trying to get data from a REST API

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error No ‘Access-Control-Allow-Origin’ header is present on the requested resource—when trying to get data from a REST API in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How No ‘Access-Control-Allow-Origin’ header is present on the requested resource—when trying to get data from a REST API Error Occurs?

Today I get the following error No ‘Access-Control-Allow-Origin’ header is present on the requested resource—when trying to get data from a REST API in javascript.

How To Solve No ‘Access-Control-Allow-Origin’ header is present on the requested resource—when trying to get data from a REST API Error ?

  1. How To Solve No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API Error ?

    To Solve No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API Error Remove this:
    credentials: 'include',

  2. No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API

    To Solve No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API Error Remove this:
    credentials: 'include',

Solution 1

Remove this:

credentials: 'include',

Solution 2

adding mode:no-cors can avoid cors issue in the api.

fetch(sign_in, {
        mode: 'no-cors',
        credentials: 'include',
        method: 'POST',
        headers: headers
    })
    .then(response => response.json())
    .then(json => console.log(json))
    .catch(error => console.log('Authorization failed : ' + error.message));
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment