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

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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

 

回答1:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!