为什么JavaScript警报在Phonegap Android应用程序中显示一个空字符串,而代码在浏览器上运行良好?
我正在开发一个简单的PhoneGap应用程序,它与运行PHP的服务器通信,获取字符串并在JavaScript警报中显示 该应用程序在浏览器上运行良好。JavaScript警报显示服务器上PHP代码返回的字符串。此操作发生在按钮的单击事件上 这是标记:为什么JavaScript警报在Phonegap Android应用程序中显示一个空字符串,而代码在浏览器上运行良好?,javascript,php,android,jquery,cordova,Javascript,Php,Android,Jquery,Cordova,我正在开发一个简单的PhoneGap应用程序,它与运行PHP的服务器通信,获取字符串并在JavaScript警报中显示 该应用程序在浏览器上运行良好。JavaScript警报显示服务器上PHP代码返回的字符串。此操作发生在按钮的单击事件上 这是标记: <!DOCTYPE html> <html xml:lang="en" lang="en"> <head> <meta content="text/html;charset=ut
<!DOCTYPE html>
<html xml:lang="en" lang="en">
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;">
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="jquery.min.js"></script>
<script src="cordova.js" charset="utf-8" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8" src="index.js"></script>
</head>
<body>
<h1>Hello World!</h1>
<button id="eventfire">Click</button>
</body>
</html>
PHP代码
$(document).ready(function(){
$("#eventfire").click(function(){
var data = {
"action": "test"
};
data=$.parseJSON('{ "name": "John" }');
$.ajax({
type: "POST",
dataType: "text",
url: "http://192.168.x.x/HelloWorldTest/response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
alert(data);
alert("type is " + typeof data + ". Length is " + data.length);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(xhr.responseText);
alert(xhr.status);
alert(thrownError);
},
statusCode: {
400: function () {
alert("Bad request!");
},
401: function() {
alert("Unauthorized!");
},
403: function() {
alert("Forbidden!");
},
404: function() {
alert("Page not found!");
},
408: function() {
alert("Request Timeout!");
},
200: function() {
alert("page reached");
},
}
});
});
})
<?php
echo 'john';
?>
当我在AVD上运行此代码时,警报不会显示字符串。我从未见过这样的警报显示出来。成功功能中显示的警报如下所示
检查这根绳子时,我知道长度是0
我能够用0个字符的字符串在浏览器上重现相同的问题
我不知道为什么这段代码在浏览器上运行正常,但在AVD上运行时却异常。该字符串按预期显示在浏览器上,但未显示在AVD上
在下面找到相同代码的屏幕截图,在浏览器上显示正确的警报
我想知道PHP字符串丢失的可能原因?我从未使用过PhoneGap,但由于您的PHP文件存储在与试图调用它的文件不同的服务器上,因此您需要启用(尽管我不能确定这是唯一的问题)
尝试在服务器的
.htaccess
文件中添加标题集访问控制允许源“*”
$(document).ready(function(){
$("#eventfire").click(function(){
var data = {
"action": "test"
};
data=$.parseJSON('{ "name": "John" }');
$.ajax({
type: "POST",
dataType: "text",
url: "http://192.168.x.x/HelloWorldTest/response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
alert(data);
alert("type is " + typeof data + ". Length is " + data.length);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(xhr.responseText);
alert(xhr.status);
alert(thrownError);
},
statusCode: {
400: function () {
alert("Bad request!");
},
401: function() {
alert("Unauthorized!");
},
403: function() {
alert("Forbidden!");
},
404: function() {
alert("Page not found!");
},
408: function() {
alert("Request Timeout!");
},
200: function() {
alert("page reached");
},
}
});
});
})
<?php
echo 'john';
?>
默认情况下,当请求来自不存储在同一服务器上的文件时,服务器会阻止对php文件等资源的访问
例如,如果我有一个js
文件位于http://myfirstwebsite.com/awesome.js
在该文件中,我对http://mysecondwebsite.com/loadstuff.php
,服务器将阻止该请求,并在控制台中产生以下错误:
XMLHttpRequest cannot load http://mysecondwebsite.com/loadstuff.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mysecondwebsite.com' is therefore not allowed access.
我不确定您是否能在PhoneGap中看到此错误,或者在哪里看到,因为我从未使用过它。
正如警告所暗示的,为了解决这个问题,您必须设置“Access Control Allow Origin”头以允许远程文件访问服务器上的文件
为了防止链接失效,以下是步骤(从该站点复制)
听起来可能很疯狂。但是你确定Android设备能够获取response.php文件吗?你能试着直接获取response.php来看看它是否有效吗?是的。访问response.php会给我指定的字符串。添加了问题的截图。只是为了确定。您是用android设备访问的,对吗?因为您的php文件是远程的,所以您是否在服务器的
.htaccess
文件中设置了标题集访问控制允许源代码“*”
?您是否尝试了其他数据类型属性,例如html或json?我认为jquery可能不支持将“text”作为数据类型。正如您在问题的评论中所建议的,我在.htaccess中添加了标题集访问控制允许源“*”。我创建了这个.htaccess文件,并将它放在我站点的根目录中,它开始工作。然后,我从该文件夹中删除了.htacess文件,但我的应用程序仍能正常工作。不知道那里发生了什么。我将尝试你在这里提出的建议,并让你知道。谢谢如果你能在回答中加上你的评论,我会接受你的回答。