IONIC 3 CORS ISSUE

痞子三分冷 提交于 2019-11-28 09:13:49

To fix this issue, please change the following lines

ionic.config.json

{
  "name": "projectLeagueApp",
  "app_id": "47182aef",
  "type": "ionic-angular",
  "integrations": {
    "cordova": {}
  },
  "proxies": [
    {
      "path":"/games",
      "proxyUrl": "https://api-2445582011268.apicast.io/games"
    }
  ]
}

You have to remove the " / " which is at the end of of "proxyUrl".

My Data Service

return this.http.get('/games/?fields=name,release_dates,screenshots&limit='+this.limit+'&offset='+offset+'&order=release_dates.date:desc&filter[genres][eq]='+genre_id+'&filter[screenshots][exists]', this.options)

In the http call, the url should begin with '/games'. '/games' because ionic will proxy http://localhost:<port>/games to https://api-2445582011268.apicast.io/games

Please use the above method for external GET and POST calls in your application.

Thank you.

If you wan to use for testing in Chrome just install chrome extension Allow control origin Quick and easy way

the proxy functionality expects that you're referencing the local server. in your GET request, you're still pointed at the remote API. If you change your code to this.http.get('/games/...' it should start to function as the request will go to http://localhost:8100/games..., which the "proxy" option will catch and pass on to the URL you've provided.

You may also only need to put https://api-2445582011268.apicast.io in the proxyUrl field. I think the rest of the path is passthrough.

To test in development environment, you can run Google Chrome in disable-web-security mode.

Steps to follow (On Windows)

  • Press Windows Key + R to open Run window.
  • Enter/input following command and press Enter key.

chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security

Steps to follow (On Ubuntu)

  • Kill all the chrome.exe instances before you run/execute it.

chromium-browser --disable-web-security --user-data-dir="[some directory here]"

  • Before Chrome 48

chromium-browser --disable-web-security

Steps to follow (On Mac)

  • Run following command in terminal.

open -n -a "Google Chrome" --args --user-data-dir=/tmp/temp_chrome_user_data_dir http://localhost:8100/ --disable-web-security

You can handle the CORS when debugging in browser by using CORS extension or by disabling the security of Chrome. But you need to handle the CORS when you debug in app on the server side, I was consuming woo-commerce API ,so i edited it as follows->

1.Plugins->editor->woocomerceapi

right after

<?php**
header(“Access-Control-Allow-Origin: *”);   

2.Update File

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!