How do I use http.get() in Flutter WEB?

Asked

Viewed 201 times

0

I’m trying to run a Flutter mobile project on WEB.

There is a part that we make a requisition http an endpoint to take a JSON data that is used the following excerpt:

final response = await http.get("http://localhost:4141/apicardapio.php?testeconexao=OK");

Mobile works correctly, but the WEB-compiled project is giving the following Exception:

message = "XMLHttpRequest error".

uri = {_SimpleUri} http://localhost:4141/apicardapio.php?testeconexao=OK
_fragmentStart = 56
_hashCodeCache = null
_hostStart = 7
_pathStart = 24
_portStart = 19
_queryStart = 40
_schemeCache = "http"
_schemeEnd = 4
_uri = "http://localhost:4141/apicardapio.php?testeconexao=OK"

Someone’s been through this trouble?

1 answer

0

To resolve the situation above, I did two things:

  1. I posted the API on a server to access via DNS instead of localhost.

  2. I inserted the header below in the file (API in PHP) that makes the above query:

    header("Access-Control-Allow-Origin: *");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.