Confused about JSON and CORS

I’m new to AngularJS and Ionic, but have some JS experience. I’m basically trying to learn both by creating a small app that would simply go to my server, retrieve some JSON data, and display it. From the demos that I have watched AngularJS does this easily and I thought this would be an easy app to try to create.

The problem is, I apparently can’t do this because it is a cross domain request (Access-Control-Allow-Origin error). I have read about JSONP and CORS, but still don’t really know what to do to get my app to talk to my web site. The web site is in a CMS and puts out a text/json feed. It’s a public feed of public posts on my site.

What do I need to do to get my app to get this data?

So first of all, I think the CORS problem usually only shows up when running ionic serve as in running your app from http://localhost:8100, and not from the emulator or a real device. (The difference is that the browser is using XMLHttpRequest to make the requests which has cross-domain implications, whereas on a device the low-level request is your standard HTTP request and doesn’t have the same restrictions - these restrictions are in place by the way because anything done in browser-javascript-land is inherently insecure so most servers by default do not trust things coming from XMLHttpRequests via domains that are not itself, but I digress…)

Secondly, the configuration involves making sure your app is sending the right CORS headers, and also making sure your server that runs your CMS is set up to allow CORS. Without knowing your tech stack (are you running your CMS on top of Apache+PHP, or something else?) I can’t give you specific advice.

I can at least link you to a blog post about CORS

1 Like

Thanks, I wasn’t looking for any type of specific advice here, just trying to understand what is going on so I can at least have a starting point to research the problem. While I do have access to the CMS, I don’t have access to the server stack, so I’m not sure how I would modify it to send CORS headers.

It has options to switch from text/json to application/json, but that’s it. Switching to application/json just gives me an unexpected token error. I will check the emulator to see if I am running into the same types of issues.