-3

重複の可能性:
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';

?>

嘆願は私を助けます。ありがとう。

4

1 に答える 1

1

以下のコードを使用してください

<div id="serverReply"><b><a href="#"  onclick="getMessage();">Get message</a></b></div>

それ以外の

<div id="serverReply" onclick="getMessage();"><b>Get message</b></div>

またはこれを試してください

<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)
    {
        alert(xmlhttp.responseText);
        document.getElementById("serverReply").innerHTML=xmlhttp.responseText;
     
    }
  }
    xmlhttp.open("GET","http://localhost/mpl/getPage.php",true);
    xmlhttp.send();
}
</script>
</head>
<body>

<div id="serverReply"><b><a href="#"  onclick="getMessage();">Get message</a></b></div>

</body>
</html>
于 2012-09-13T11:31:17.907 に答える