0

I'm trying to get the content of a page with AJAX and I get no result..

this is the code:

<script type="text/javascript">
function onSumResponse() {
var xmlhttp;
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("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","http://www.arihav.com/",true);
xmlhttp.send();
}

</script>

And this is the div in the body:

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="onSumResponse()">Change Content</button>

What I get after the click is blank div.

EDIT: This code was taken from w3schools

EDIT 2 : This is the code in vbscript that works:

GotothisURL = "http://www.arihav.com"
Set GetConnection = CreateObject("Microsoft.XMLHTTP")
GetConnection.Open "get", GotothisURL, False
GetConnection.Send
ResponsePage = GetConnection.responseText
Amir Tugi
  • 2,386
  • 3
  • 16
  • 18
  • did you check the response coming from the ajax call? do you even get any data? – Tomer Jul 08 '12 at 14:21
  • 2
    It's probably a cross-domain request, so it probably won't work. Also, the fact that you have a "code for IE6, IE5" comment is bad. – Ry- Jul 08 '12 at 14:21
  • @ftom2 : What do you mean by check the response? I have changed the content of the div according to the function. Isn't it enough? – Amir Tugi Jul 08 '12 at 14:24
  • Do an alert or console.log on the xmlhttp.responseText, my guess is that it is coming empty, and then the problem is on the server side not sending the data. also, if you can, i'd suggest you start using jquery, it'll make your life much easier. – Tomer Jul 08 '12 at 14:26
  • The alert has returned empty. This is truly weird because I have this simplest code in vbscript that works, but I can't get anything else to work. (I need it in PHP). – Amir Tugi Jul 08 '12 at 14:32
  • As @minitech said, it looks like a cross-domain request, which means it won't work unless the requested server explicitly allows such requests – Mitya Jul 08 '12 at 14:35
  • Please take a look in my second edit. That code works but it's in vbscript. – Amir Tugi Jul 08 '12 at 14:40

1 Answers1

0

AJAX does not allowed to access a different server due to origin policy if you want to request another host better use curl or JSON

Community
  • 1
  • 1
itsme
  • 575
  • 1
  • 6
  • 15