Ajax readyState always 1

I'm tying to do what seems like a simple ajax but can't get it to work. Here's my code:

var xmlHttpRequest;

function processRequest(){
 alert("process request called with " + xmlHttpRequest);
 if(xmlHttpRequest.readyState==4){
  alert("status = " + xmlHttpRequest.status);
  if(xmlHttpRequest.status == 200){    
     }
   } else {
    alert("process request no luck readyState = " + xmlHttpRequest.readyState);
   }
 alert("process request exiting");
}


function updateCount(customerID, productID) {

 xmlHttpRequest = init();

  function init(){

 if (window.XMLHttpRequest) {
               return new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } else {
         alert("Your browser does not support AJAX!");
        }
  }


 xmlHttpRequest.open("GET", url, true); 

 xmlHttpRequest.onreadystatechange = processRequest(); 
}

Like I said in the subject line, readyState is always 1. What am I doing wrong?

Thanks! Eddy

Answers


You are calling processRequest before you start your request.

xmlHttpRequest.onreadystatechange = processRequest();

is wrong and has to be

xmlHttpRequest.onreadystatechange = processRequest;

This will store a reference to your method instead of calling it directly.

As soon as the ready state changes, the xmlHttpRequest object trys to call this reference.


Add xmlHttpRequest.send(); after xmlHttpRequest.onreadystatechange = processRequest;.


Need Your Help

Getting the last word from a string

c arrays

In a assignment I need to be able to find the last word from a two dimensional array of strings (the last word for each line/string) so I can apply a cipher to it. I have tried this:

Getting Google plus profile image

java google-plus

I have been developing a Java language application.