Javascript phonegap+;php不工作
我想用eclipse和android emulator从phonegap中的php加载文本。 我不知道我做错了什么 my code index.html:Javascript phonegap+;php不工作,javascript,php,android,cordova,Javascript,Php,Android,Cordova,我想用eclipse和android emulator从phonegap中的php加载文本。 我不知道我做错了什么 my code index.html: <html> <head> <script src="cordova.js"></script> <script> var xmlhttp; window.onload = function(){ document.ad
<html>
<head>
<script src="cordova.js"></script>
<script>
var xmlhttp;
window.onload = function(){
document.addEventListener("deviceready", init, false);
};
function init(){
var url = "http://localhost/php-handyAPP/phpinfo.php";
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
alert ( xmlhttp.readyState);
if(xmlhttp.readyState == 4 && xmlhttp.readyState == 200)
{
document.getElementById("result").innerHTML = xmlhttp.responseText;
} else {
document.getElementById('result').innerHTML = "Error loading page ";
}
};
xmlhttp.open("GET", url , true);
xmlhttp.send(null);
};
</script>
<title>phonegap + php</title>
</head>
<body>
<div id="result"></div>
</body>
</html>
var-xmlhttp;
window.onload=函数(){
文件。附录列表(“DeviceRady”,初始,错误);
};
函数init(){
变量url=”http://localhost/php-handyAPP/phpinfo.php";
xmlhttp=新的XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
警报(xmlhttp.readyState);
if(xmlhttp.readyState==4&&xmlhttp.readyState==200)
{
document.getElementById(“结果”).innerHTML=xmlhttp.responseText;
}否则{
document.getElementById('result').innerHTML=“加载页面时出错”;
}
};
open(“GET”,url,true);
xmlhttp.send(空);
};
phonegap+php
我的php代码-phpinfo.php:
<?php
echo 'Hello World';
?>
my config.xml
<access origin="*"/>
my AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
我从android emulator中的if语句“错误加载页面”中获得。PhoneGap没有PHP服务器 在试图加载位于localhost的php页面的代码中:
var url=”http://localhost/php-handyAPP/phpinfo.php";代码>
在这种情况下,Localhost是您的仿真器,我猜仿真器上没有PHP服务器
如果PHP托管在您的本地计算机上(运行emulator的地方),那么您可以尝试:
var url=”http://10.0.2.2/php-handyAPP/phpinfo.php";代码>相反,但我不保证它会起作用
可能重复:您应该在要调用的php文件中添加此代码
添加它,它肯定会工作。可能因为url包含localhost,所以该网站仅在您的pc上运行,无法从您的网络或internet访问。您最有可能提出跨域请求,这自然会失败。查看并调整您的phonegap和xmlhttp请求URL.my PHP(xampp)。托管在我的机器上(模拟器也在运行)。你是对的,它不适用于var url=“”;或var url=“”;我已经对它进行了测试,但不幸的是,它不能再工作了。用ajax jquery试试它会工作的,我不知道javscript ajax,所以试着用它。