CORS errors when using AJax


#1

I’m getting this error when I call an Ajax function to a PHP to insert records or retrieve data, though occasionally sometimes the data will get through:

has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

I’ve read up a bit about it, and it was working fine more or less on a free server which was automatically protected under SSL. I’ve since purchased a dedicated domain without SSL and now I’m hitting this error again. Would adding SSL fix this? or is this something the hosting company can only answer?

I do actually have a SSL directory in my root folder of my website - I’m not really up too much with this, should the PHP scripts be accessed from there maybe?

I originally got around the problem on the free site by adding the following header to my PHP files, but that’s not working in this instance:

header('Access-Control-Allow-Origin: *');

One final note, it was working without problems on an actual Android device - just not in Chrome - is CORS only relevant to browsers, or am I likely to hit problems once I try to roll the app out to iOS and phones with newer versions of Android?


#2

I am no expert on configuring servers and CORS policies, but you’ll need to worry about this on your PhoneGapped app. The ContentSecurityPolicy (in Project Properties) needs to be correct.


#3

Ok, thanks.

I’ve only just had a chance to run my app on my phone in debug mode on Chrome - The only error I’m getting is:

Could not find cordova.js script tag. Plugin loading may fail.

Which I’m guessing is the reason for the data not getting inserted into my database.


#4

That message is OK. It looks for phonegap.js and cordova.js, which do the same thing. You’ll need to put some tracing in (use console.log) to see what is happening.