可能重复:
XmlHttpRequest 错误:Access-Control-Allow-Origin 不允许 Origin null
我是 JavaScript Phonegap 和 AJAX 的新手。我正在尝试编写一个简单的 Phonegap 应用程序,该应用程序将向服务器请求消息,但该应用程序没有响应。当我在 chrome 浏览器上将脚本作为文件运行时,因为我了解 Phonegap 的工作原理,它显示了以下内容XMLHttpRequest cannot load http://localhost/mpl/getPage.php. Origin null is not allowed by Access-Control-Allow-Origin.
我怎样才能解决这个问题?我的代码在下面。
<html>
<head>
<script type="text/javascript">
function getMessage()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("serverReply").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http://localhost/mpl/getPage.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="serverReply" onclick="getMessage();"><b>Get message</b></div>
</body>
</html>
我getPage.php
的很简单
<?php
echo 'cool';
?>
请帮助我。谢谢。