1

I have the following generic Ajax function:

//run post request
function ajaxPost (divid, parameters, file) {

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() {
    alert ("ok")
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      alert ("ready");
      alert (xmlhttp.responseText);
    divid.innerHTML=xmlhttp.responseText;
    }
  }

  xmlhttp.open("POST", file,true);

  //Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", parameters.length);
xmlhttp.setRequestHeader("Connection", "close");



xmlhttp.send(parameters);
}

The problem being that this section does not work as expected:

xmlhttp.onreadystatechange=function() {
    alert ("ok")
  if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      alert ("ready");
      alert (xmlhttp.responseText);
    divid.innerHTML=xmlhttp.responseText;
    }
  }

In browser I get muliple "ok" alerts as I expected but the statements in the if statement never fire. I take this to mean the php is returning state updates but for some reason is never returning ready codes. How that can happen - I have no idea.

Can anyone tell me why I wouldn't recieve the ready codes?

The php itself is not the problem:

<?php

echo "new";

?>

I have tested the function input (divid, parameters and file), and these are ok. This function was previously working in a seperate project.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
YsoL8
  • 2,186
  • 5
  • 29
  • 47
  • So it only goes until state 3? Inspect the different values of `readyState`... – Felix Kling Dec 14 '10 at 10:19
  • @Felix King just tested that. It reaches state 4 but status returns 404 immediately after that happens. – YsoL8 Dec 14 '10 at 10:23
  • Also inspect `status`. Maybe you're getting a 404. Edit: there you go. – Gipsy King Dec 14 '10 at 10:24
  • @YsoL8: So that is your problem. `xmlhttp.readyState==4 && xmlhttp.status==200` evaluates to `false` then. Maybe you have the wrong URL? Or your server is not set up properly. – Felix Kling Dec 14 '10 at 10:25
  • 1
    I now know I am getting a 404. I've not encountered this problem before and I don't know how to fix it. – YsoL8 Dec 14 '10 at 10:26
  • @YsoL8: Can you access the URL in the browser? You probably will get a 404 too. What does it say? – Felix Kling Dec 14 '10 at 10:27
  • I am using a file in the same folder using this url: test.php - I don't imagine that being wrong! Also I can browse to the file directly – YsoL8 Dec 14 '10 at 10:27
  • The `file` parameter, which you use as URL parameter to `xmlhttp.open`, seems to be wrong. Try using firebug and `console.log()` to debug instead of `window.alert()`. – Gipsy King Dec 14 '10 at 10:36
  • You mean coded wrong or being passed the wrong url? – YsoL8 Dec 14 '10 at 10:43

4 Answers4

1
xmlhttp.open("POST", file,true);

file is URL right?
try to echo from that URL.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
James
  • 11
  • 1
0

I would hazard a guess that your server is not returning a valid 200 response, therefore the if statement xmlhttp.readyState == 4 && xmlhttp.status == 200 will never equate true.

Try using Firebug to see what request is being made (if any) and what response you get back. If you don't use Firefox, try using a proxy tool like Charles.

Further to all this, I would highly recommend using a stable open source Javascript library such as jQuery. This has AJAX code built in (among other things) which is all rigorously tested. Not only will this save you time, it will eliminate further possibility of errors from your own code, e.g.,

$.get('url.php', function(data) {
    // 'data' is responseText
});
Stephen Melrose
  • 4,772
  • 5
  • 29
  • 42
0

xmlhttp.readyState && xmlhttp.status depend on the browser used, not the PHP script. Worst case, the PHP script might not resturn Status 200, and if this happens means that you either have an error in your script(sometimes this leads to Status 500) or the server returnr a 404. In any case, try accessing the url directly, or look in the firebug log window, it may be easier to debug.

Quamis
  • 10,924
  • 12
  • 50
  • 66
-1

Sorry but the problem is very simple. It took me a whole week.

xmlhttp is not the same as xmlHttp.

Mike Fielden
  • 10,055
  • 14
  • 59
  • 99