# \[CLI3\] Yet Another CORS Problem

**URL:** <https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101>\
**Category:** ionic-v3\
**Created:** [March 19, 2018, 10:07am UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101 "2018-03-19T10:07:03Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![DovahBear](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@DovahBear](https://forum.ionicframework.com/u/DovahBear)\
**Post date:** [March 19, 2018, 10:07am UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/1 "2018-03-19T10:07:03Z")

</div>

Hey all! I’m trying to run my Ionic 3 app on my phone. I seem to be having some trouble connecting to an API using HTTP requests. I’m running into the infamous CORS problem. I’ve tried almost everything I read around the net and nothing seems to be working; In browser using `ionic serve` or on mobile using `ionic cordova run android --livereload`.

Yes, I did try adding a proxy:

```auto
{
  "name": "MyApp",
  "app_id": "",
  "type": "ionic-angular",
  "integrations": {
    "cordova": {}
  },"proxies": [
    {
      "path": "/subscribe",
      "proxyUrl": "https://www.ServerURLHere.com/subscribe"
    }
  ]
  
}

```

Yes, I did download the Chrome extension [Extension Allow-Control-Allow-Origin: \*](https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en).

But no luck whatsoever. Here is the console log from inspecting the app running on my Android phone:

 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/5/1/5118dee6989362b9da8da3d77f26f185b4c54b58.png)

Here’s the HTTP Request I’m posting:

```auto
    let method = '/subscribe';

    let data : any = {"name":"Jane Doe"},

    ContentType : string = "application/x-www-form-urlencoded",
    UserAgent : string = "request",
    Auth : string = this.AccessToken,
    Accept : string = "application/json",

    headers : any = new Headers({
                                        'User-Agent': UserAgent,
                                        'Authorization': Auth,
                                        'Accept': Accept,
                                        'Content-Type': ContentType
                                      }),
    options : any = new RequestOptions({ headers: headers }),
    url : any = this.MyURL + method;

    this.http.post(url, data, options) // Request stops here. Code doesn't even reach the console.logs.
    .subscribe(Feed =>
    {
      if(Feed.status === 200)
        console.log("Got the feed!");

      else
         console.log('Something went wrong!');
    });

```

Any help would be GREATLY appreciated!

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [March 19, 2018, 11:56am UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/2 "2018-03-19T11:56:17Z")

</div>

Read the error message. It tells you exactly which response header is missing and why this is blocking your request. Add that header, problem solved.

---

<div class="post-metadata">

**Author:** ![DovahBear](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@DovahBear](https://forum.ionicframework.com/u/DovahBear)\
**Post date:** [March 19, 2018, 1:35pm UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/3 "2018-03-19T13:35:34Z")

</div>

Hey Sujan!  
Thanks for your answer. Taking another look at the error it says that there is no ‘Access-Control-Allow-Origin’ header on the requested resources. From what I personally understand that means the API I’m calling is missing the above header. Is that correct?  
But if you mean to say that I should include the header in **my** request payload, then I’ve already attempted to do so as such:

```auto
    headers : any = new Headers({
                                        'Access-Control-Allow-Credentials' : true,
                                        'Access-Control-Allow-Origin': this.MyURL, // Format: www.MyURL.com/api
                                        'User-Agent': UserAgent,
                                        'Authorization': Auth,
                                        'Accept': Accept,
                                        'Content-Type': ContentType
                                      }),

```

Which has spawned a similar error:  
 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/3/3/335cd7b290ed90d8d50c795019ea70212ef043b0.png)

Please advise. Thank you.

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [March 19, 2018, 4:01pm UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/4 "2018-03-19T16:01:54Z")

</div>

No, the API has to respond with that header - it allows a specific Origin (in this case the one of the Ionic app) to get the data the request returns. Otherwise the “browser” (the webview in this case) will not forward the result to your app and throw this error message.

Many APIs allow you to set that value. If not, you will have to find a workaround (like a proxy on your server that does the actual request, adds the header and returns that to your app).

---

<div class="post-metadata">

**Author:** ![DovahBear](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@DovahBear](https://forum.ionicframework.com/u/DovahBear)\
**Post date:** [March 19, 2018, 4:33pm UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/5 "2018-03-19T16:33:20Z")

</div>

Unfortunately, I do not have control over this API whatsoever; I’m simply calling data from it.  
I was able to make these calls via a NodeJS project, but the attempt was fruitless via Ionic.  
Also as I mentioned in the first post, I did try to use a proxy to fix this problem but it didn’t affect the Ionic app’s behavior.

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [March 19, 2018, 4:36pm UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/6 "2018-03-19T16:36:27Z")

</div>

Read what I wrote again.

NodeJS doesn’t work in the context of a browser with the security restrictions this environment adds - so this is not an issue there. The proxy you tried to use is a different thing than what I suggested and doesn’t really help here.

---

<div class="post-metadata">

**Author:** ![DovahBear](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@DovahBear](https://forum.ionicframework.com/u/DovahBear)\
**Post date:** [March 19, 2018, 9:23pm UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/7 "2018-03-19T21:23:38Z")

</div>

Ah I did re-read what you posted and I’m not too excited about it ☹

Payload path:  
Ionic App --\> My Server (with proxy) --\> API  
And the response would be take the same road back to the app. Correct?  
Doesn’t seem very… ‘Efficient’ might be the word? Is there no other way to do it?

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [March 19, 2018, 9:32pm UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/8 "2018-03-19T21:32:10Z")

</div>

If is a workaround for a security feature, those are not meant to be efficient 😛

[https://ionicframework.com/docs/native/http/](https://ionicframework.com/docs/native/http/) might be an alternative.

---

<div class="post-metadata">

**Author:** ![DovahBear](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@DovahBear](https://forum.ionicframework.com/u/DovahBear)\
**Post date:** [March 19, 2018, 9:40pm UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/9 "2018-03-19T21:40:34Z")

</div>

AdvancedHttp? Sorta looks like the one that ships with ionic currently but I’ll gladly give it a try tomorrow! Thanks!

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [March 19, 2018, 10:03pm UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/10 "2018-03-19T22:03:09Z")

</div>

Ionic’s `http` is Angular’s http which is in TS/JS - native HTTP is… native code. Outside the context of the webview, so outside of these security considerations (at the cost of performance and complexity of course).

---

<div class="post-metadata">

**Author:** ![DovahBear](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@DovahBear](https://forum.ionicframework.com/u/DovahBear)\
**Post date:** [March 20, 2018, 9:08am UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/11 "2018-03-20T09:08:23Z")

</div>

Worked like a charm! Thank you so much for your help! 😃

---

<div class="post-metadata">

**Author:** ![hjchin](https://avatars.discourse-cdn.com/v4/letter/h/db5fbb/32.png) [@hjchin](https://forum.ionicframework.com/u/hjchin)\
**Post date:** [March 24, 2018, 3:52pm UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/12 "2018-03-24T15:52:41Z")

</div>

[https://ionicframework.com/docs/native/http/](https://ionicframework.com/docs/native/http/) only supports iOS and Android. In case you want to add browser platform, it won’t run. Check the link. [https://ionicframework.com/docs/native/http/#platforms](https://ionicframework.com/docs/native/http/#platforms)

the CORS issue is caused by option --livereload. I encountered same issue last time. Remove it and it shouldn’t give you any trouble.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:44pm UTC](https://forum.ionicframework.com/t/cli3-yet-another-cors-problem/125101/13 "2021-07-19T21:44:32Z")

</div>


