Home > Back-end >  Getting "XMLHttpRequest" error while using Chrome Web on VS Code
Getting "XMLHttpRequest" error while using Chrome Web on VS Code

Time:12-20

When I launch my application on Chrome Web using VS Code and try to send a http request to the Backend side, it gives me XMLHttpRequest error and does not work.

When I launch the app on Android Emulator it works. Also I tried to add Allow-All-Origins and other CORS options in my Backend application but still I get the same error.

I also tried to deactivate CORS on Chrome Web but it also didn't work. I don't know if is it necessary to add what solutions I tried and didn't get the answer or not, but I also like to know if is it a Bug or a problem with Flutter or Chrome web?

CodePudding user response:

I changed the following line of the code:

final url = Uri.parse('http://10.0.2.2:8000/api/$urlSegment');

Like below:

final url = Uri.parse('http://127.0.0.1:8000/api/$urlSegment');

And the problem seems to be solved, but I am not sure if it is the correct solution or not?

CodePudding user response:

You need to add code in the header of the backend file.

add or change in your header code

"Access-Control-Allow-Origin": "*",

"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE, HEAD",

  • Related