Http get Request always must fired twice to work? Help pls

What’s the full error message you got on the phone? Also, could simply do the following, which is basically the same as what you’re already doing, just neater

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

Some people might be really against just allowing all origins though, personally I don’t really understand why that matters, at least in this scenario.

Edit: Either that, or proxy your requests through an origin which is allowed.

#Much later edit, quite important:
Right. Did some searching and reading, so in case anyone stumbles upon this later: You should ONLY have the Access-Control-Allow-Origin: * set when you’re developing using your browser, especially if the API is not only used for the app but also some other web based application! This is not required for the phone, as the files are served from the file:// URI and cross domain policies do not apply. Either you set the ACAO on the server, or setup a proxy in your ionic.config.json file during development

{
  "name": "",
  "app_id": "",
  "v2": true,
  "typescript": true,
  "proxies": [{
    "path": "/api",
    "proxyUrl": "https://example.com/api"
  }]
}

Then call it like this

this.http.get("/api/...", ...)...
1 Like