-1

I am trying to retrieve data(string) from a flask server with the a GET request and the xhttp.responseText is always an empty string.

Here is my python code

Here is my Html code

edit: console tab says: Access to XMLHttpRequest at 'http://127.0.0.1:5000/' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

  • Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Aug 17 '22 at 19:14
  • Also look in the console and network tab for errors – mplungjan Aug 17 '22 at 19:15
  • the console tab says: Access to XMLHttpRequest at 'http://127.0.0.1:5000/' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. I am not really sure what this mean – Samit Manjunath Aug 17 '22 at 19:20
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 17 '22 at 19:27
  • 1
    Your backend needs to return proper CORS headers, like the Access-Control-Allow-Origin mentioned in the error. https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-receive-a-no-access-control-allow-origin-header-i – James Aug 17 '22 at 19:33

1 Answers1

-1

Instead of triggering the GET from your front-end, can you build the same GET request in Postman and check if it still returns an empty string? (i.e. as a way to debug the issue)

See: Building requests

Omair
  • 11
  • 2
  • I built the same GET request in postman and it still return an empty string. – Samit Manjunath Aug 17 '22 at 19:37
  • Can you inspect the GET request details in Postman and check what the `Origin` field is? Also, in the server's HTTP response, do you see a `Access-Control-Allow-Origin` field? What is it set to? – Omair Aug 17 '22 at 20:00
  • `Access to XMLHttpRequest at '127.0.0.1:5000' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource` – mplungjan Aug 17 '22 at 20:00
  • @mplungjan - thanks for pointing that out. – Omair Aug 17 '22 at 20:25