How to use HTTP request to external domain?

Hi,

I’m beginer in IONIC 2, I’m use Http request of angular2 to call to external domain from app, but I’ve got error:

XMLHttpRequest cannot load http://api.***.io/api/hot-content?__jsonp_callback=864658544808.7673. Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8100’ is therefore not allowed access.

Please help me know how to call as JSONP (same on jQuery ajax).

Thanks,

it occurs because of CORS… you are accessing data from another domain. To rectify it you have the add a meta tag at your index.html head body. For ex see below.
<meta http-equiv="Content-Security-Policy" content="default-src *; script-src &apos;self&apos; &apos;unsafe-inline&apos; &apos;unsafe-eval&apos; *; style-src &apos;self&apos; &apos;unsafe-inline&apos; *; img-src 'self' data:;">
You can use this tag for json data. if you are trying to view image or other form of data it will not work. For further details google out how to set Content Secure policy .

1 Like

Hi,

I’ve change the server API response to add flowing code and now it work well:

header(‘Access-Control-Allow-Origin: *’);
header(‘Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS’);
header(‘Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token, X-Request-Token, X-Request-AppId’);

Thanks harshit886