views:

162

answers:

2

My Problem


xmlhttp.readyState is always 2 then 3 in firefox and other browsers in Internet Explorer the opposite, and 1 and 4 is missing.

My Code


function like(id,type){
    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() {
            var element = document.getElementById('answersLoader'+id);
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var result = xmlhttp.responseText; // the result from AJAX response
            } else {
                // FOR stackoverflow users: I've checked here the state and status with this: 
                // alert(' state: ' + xmlhttp.readyState +' status: '+xmlhttp.status); 
                // The status result is always 200 but the state firstly is 2 then 3 in firefox and other browsers, in IE the opposite.

               element.innerHTML="<img src=\"http://localhost/images/default/icons/loader.gif\" />";

            } 
        }

        xmlhttp.open("GET", "http://localhost/ajax/like.php?id="+id+"&amp;type="+type, true);
        xmlhttp.send(); 
}
+2  A: 

Ajax calls are notoriously inconsistent amongst browsers. The answer is to use a library to cover up the inconsistencies for your Javascript sw.

You can copy the appropriate code from a library into your code if you really want to, or you may well find that the other parts of the library will also be of help.

I prefer YUI. JQuery and Mootools also have large user bases and communities

Larry K
First of all this is not an answer for my question second I hate all libraries, I write my own code, third if you don't know the answer don't post one.
CIRK
The answer is the first line in my answer: "Ajax calls are notoriously inconsistent amongst browsers." Sorry if you don't like it.
Larry K
The questions was: `Why I’m NOT getting xmlhttp.readyState 1 and 4 ?`, and your answer `is Ajax calls are notoriously inconsistent amongst browsers.` This is not an answer for my question.
CIRK
A: 

The problem was in my PHP file not in the ajax.

CIRK