PhoneGap的无“访问控制 - 允许 - 原产地”标头的请求的资源present。原产地'空',因此没有允许访问 [英] Phonegap No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access

查看:175
本文介绍了PhoneGap的无“访问控制 - 允许 - 原产地”标头的请求的资源present。原产地'空',因此没有允许访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PhoneGap的我有一个问题,同时呼吁使用jQuery AJAX的PHP文件。而调用文件时出现的错误是:

  

没有访问控制 - 允许 - 原产地标头是present所请求的资源

谷歌表示,把这个code 头(访问控制 - 允许 - 产地:*'); 在当前的PHP文件头。我把code在头,但仍然没有改变。我重视我的 jsfidde 的吧。

文件 connectionsqlphp.php code是这里

 < PHP

标题(访问控制 - 允许 - 产地:*');
标题(内容类型:应用程序/ JSON);
回声嗨;

?>
 

解决方案

应该有,只要你正确地配置访问起源于config.xml文件中的PhoneGap /科尔多瓦无交叉的起源问题。

这是正常的,你有这样的消息从浏览器进行测试时,如果你错过了一些CORS headers.Try加入:

 头(访问控制 - 允许 - 产地:*');
标题(访问控制 - 允许 - 方法:GET,POST);
 

不过,再一次你并不需要这些头为PhoneGap的应用程序。

当你这样做回声嗨; ,您需要的资料不发送JSON,但发送文本嗨。 (只是写JSON的头不格式化你回声)。

我觉得你不应该使用 $获得中的JavaScript和删除标题(内容类型:应用程序/ JSON'); 从PHP这样的应用程序需要一个简单的文本或修改你的PHP真正发送JSON。

为前:

 回声{消息:\你好\};
 

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

这篇关于PhoneGap的无“访问控制 - 允许 - 原产地”标头的请求的资源present。原产地'空',因此没有允许访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆