Phonegap The requested resource has the header "Access-Control-Allow-Origin". The origin of 'null' therefore does not allow access

In a telephone bundle, I have a problem when calling a PHP file using jQuery AJAX. Error calling file:

No header "Access-Control-Allow-Origin" is present on the requested resource

Google said to place this header('Access-Control-Allow-Origin: *'); code header('Access-Control-Allow-Origin: *'); in the current PHP file header. I put the code in the header, but still no change. I attached jsfidde to it.

connectionsqlphp.php code here

 <?php header('Access-Control-Allow-Origin: *'); header('Content-Type: application/json'); echo "hi"; ?> 
+7
json jquery ajax php cordova
source share
1 answer

There should be no cross-origin issues in phonegap / cordova if you correctly configure the source code in the config.xml file.

It’s normal that you have this message when testing in a browser if you missed some CORS headers. Try adding:

 header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST'); 

But again, you do not need these headers for the phone application.

When you do echo "hi"; , yours does not send json, but sends the text "hello". (just a json entry in the header does not format what you echo).

I think you should either use $.get in javascript, and remove the header('Content-Type: application/json'); from php so that the application expects plain text or modifies your php to really send JSON.

for ex:

 echo "{message:\"hi\"}"; 
+8
source share

All Articles