Local vs production/phone environment detection


#1

I’m currently faced with a problem. Within my application I make HTTP requests to an external API which I have white listed within my config.xml file. Now on the phone, the requests are made and the JSON is returned. However, when on Chrome (w/ ionic serve) I cannot make these requets due to CORS.

Coming from a PHP Framework background, you’d get around something like this by settings an environment variable on the server and then the application can detect if it’s production or local, and from there create a separate task. If this were the case, I could create a local proxy server which completes the request with cURL (or something), and get my local environment (Chrome) to send the requests to that (no cross domain involved then).

This is all well and good, but how can I detect between local (Chrome via ionic serve) and it being production (built onto a mobile device).

Thanks!


#2

Your best bet is to configure the server to properly handle CORS. However, if you have no control over the server, you’re not going to be able to do that.

You can start Chrome in a mode that prevents security checks.

On a Mac : open -a Google\ Chrome --args --disable-web-security

Reference : http://stackoverflow.com/a/6083677/75644


#3

I wouldn’t say I have no control over the server, but allowing access to any CORS request is not my idea of a solution to be honest. I did find this app:

Seems to be working, just need to figure out Ionic properly now haha.