0

I'm trying to fetch a string from a URL and print it in the console, here's the code:

function myFunction() {

    const url = 'http://127.0.0.1:5000/' + makeOutputId + "," + modelOutputId + "," + userInputYear + "," + userInputTransmissionId + "," + userInputFuelTypeId
    + "," + userInputEngineCapacity + "," + userInputMileAge;

    console.log(url);
    fetch(url)
    .then(function (response) {
        response.text().then(function (text) {
            storedText = text;
            console.log(storedText);
        });
    }); 
}

When I just click on the URL, it works but when I try fetching it I'm getting the error mentioned in the title. Is there anything wrong with my code? I tried going through other answers from similar questions but none of them was helpful.

Abdullah Ajmal
  • 431
  • 5
  • 17

1 Answers1

0

If you have access to backend settings, try to add 2 headers as something like below:

$response = $response->withHeader('Access-Control-Allow-Origin', '*');
$response = $response->withHeader('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');

For getting more information https://www.google.com/search?q=cors+policy

Evgeny Gurevich
  • 475
  • 3
  • 5
  • There's no reason to set *any* of those `Access-Control-Allow-Headers` values for this request, and never any reason to set `Access-Control-Allow-Headers` as an allowed request header. – Quentin Jul 08 '21 at 12:09