0

(ionic 3 app)

let me walk you through, whats going on here. i have a file information.json, which contains information about various categories.

{
    "items": [
        {
            "name":"Phones and Tablets",
            "category_id": "20",
            "image": "http://somesite/image/cache/catalog/menu-icons/phone-tablet-icon-500x500.png",
            "children": [
                {
                    "name": "Smartphones",
                    "category_id": "60"
                },
                {
                    "name": "Tablets",
                    "category_id": "62"
                },
                {
                    "name": "Accessories",
                    "category_id": "63"
                },
                {
                    "name": "Basic Gsm Phones",
                    "category_id": "64"
                }
            ]
        },

which is just sitting there looking pretty in main directory on my site. when i maka a get request.

var headers = new Headers();
    headers.append('accept', 'application/json');
    let options = new RequestOptions({ headers: headers, });
    this.http.get("http://somesite/information.json", options)
    .map(data => data.json().items)
    .subscribe(data => {
      this.information = (data);
      console.log(this.information);
       }
    )}

i get CORS error in console. Failed to load http://somesite/information.json: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

i have a Moesif origin & Cross changer chrome extension, when i enable that then everything works fine in browser. when i build/run the app on android everything seems to work fine. but when i run the same app on ios, it doesn't seem to get any response. i dont know if ios block all CORS by default or something. i tried fiddling around with .htaccess file of my site, i included Headers Allow-Access-Control-Origin "*", which seems to work for only 1 request, so i was able to get categories information. but all other get requests from my app became invalid. currently i am looking around to set up a PHP file with headers information so when i make a get request to php, it echos out that file information.json , but the problem is i just dont know how to set it up properly. if you guys have any other solution for this problem, feels free to share them.

thanks.

Farhan Ullah
  • 58
  • 1
  • 5
  • You use js to get a cross domain file http://somesite/information.json. – LF00 Nov 19 '17 at 08:32
  • Possible duplicate of [Origin is not allowed by Access-Control-Allow-Origin](https://stackoverflow.com/questions/10143093/origin-is-not-allowed-by-access-control-allow-origin) – Kewin Dousse Nov 19 '17 at 09:05

0 Answers0