How to resolve CORS issue and JSONP issue in Ionic2

自古美人都是妖i 提交于 2019-12-06 14:10:08

You can disable same origin policy in Chrome.

  1. Create a separate chrome icon on your desktop.

  1. Rename that icon to chrome (x-domain) so you know which is which.

  1. Right click on your new icon and click properties.

  1. Change the target field to "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-agent="Android" --user-data-dir="C:/temp-chrome-eng"

  1. Click Ok.

The magic happens here:

--disable-web-security

When you open your browser it will look like this:

WARNING: ONLY USE FOR TESTING PURPOSES AS THIS BROWSER HAS SECURITY DISABLED.

Kuba Beránek

The error you're receiving is because your browser is blocking the cross-origin AJAX requests (the domain doesn't respond with the required headers). However if you execute the application on a mobile device, it should work, because the application will execute the request directly. You can read more here. Does it work if you launch the app on a mobile device?

I am running on browser using CORS extensions and plugins on both chrome and mozilla. still there is no success.

I'm not getting whether my code is wrong or something else that matters.

EDIT: Follow below two comments to disable web security of chrome on MAC machine.

i had problem to use ionic and iis on same url ( localhost:8100 ) this chrome extension help me to solve cors issue: [enter link description here]

https://chrome.google.com/webstore/detail/moesif-origin-cors-change/digfbfaphojjndkpccljibejjbppifbc

also for web api you can put this peace of code inside system.webServer tag in your web.config file

<system.webServer> 
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, 
DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
...
</system.webServer>

make sure your header is correct:

let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Access-Control-Allow-Origin','*');
headers.append('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');
headers.append('Access-Control-Allow-Headers','Content-Type');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!