Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access

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";

?>
like image 591
Sri Avatar asked Feb 14 '14 07:02

Sri


1 Answers

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\"}";
like image 60
QuickFix Avatar answered Oct 20 '22 17:10

QuickFix