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

1
  • "When I run my script on chrome browser as a file[...]" XMLHttpRequest isn't allowed on file:// protocols Commented Sep 13, 2012 at 11:25

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.