-3

Possible Duplicate:
XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

I'm a newbie to JavaScript Phonegap and AJAX. Am trying to write a simple Phonegap app that will request for a message from a server however the app does not respond. When I run my script on chrome browser as a file because I understand that is how Phonegap works it shows the foll XMLHttpRequest cannot load http://localhost/mpl/getPage.php. Origin null is not allowed by Access-Control-Allow-Origin.

How can I fix this? My code is down below.

<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>

My getPage.php is simple it's just

<?php

echo 'cool';

?>

Pleas help me. Thanks.

Community
  • 1
  • 1
sammyukavi
  • 1,501
  • 2
  • 23
  • 51

1 Answers1

1

use the code below

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

instead of

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

Or try this

<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>
Univers3
  • 935
  • 1
  • 13
  • 34
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53