In phonegap I have an issue while calling a PHP file using jQuery AJAX. The error that occurs while calling the file is:
No 'Access-Control-Allow-Origin' header is present on the requested resource
Google said to put this code header('Access-Control-Allow-Origin: *');
in the current PHP file header. I put the code in the header, but still no changes. I've attached my jsfidde with it.
The file connectionsqlphp.php
code is here
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
echo "hi";
?>
There should be no cross origin issue in phonegap/cordova as long as you configure properly access origin in config.xml.
It's normal that you have this message when testing from a browser if you miss some CORS headers.Try adding :
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
But once again you don't need those headers for a phonegap app.
When you do echo "hi";
, your're not sending json but sending a text "hi". (just writing json in the header does not format what you echo).
I think you should either use $.get
in the javascript and remove header('Content-Type: application/json');
from php so that the app expects a simple text or modify your php to really send JSON.
for ex:
echo "{message:\"hi\"}";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With