重複の可能性:
XmlHttpRequestエラー:OriginnullはAccess-Control-Allow-Originで許可されていません
私はJavaScriptPhonegapとAJAXの初心者です。サーバーからのメッセージを要求する単純なPhonegapアプリを作成しようとしていますが、アプリが応答しません。Phonegapがどのように機能するかを理解しているため、Chromeブラウザーでスクリプトをファイルとして実行すると、次のように表示されます。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';
?>
嘆願は私を助けます。ありがとう。