JavaScript XMLHttpRequest.onreadystatechange

I am trying to do AJAX and need to know why this code does not run a complete or warning error message. I'm in Mozilla Firefox 20.0.1

NOTE

This IS code updates the database (I have a select statement that reads the exact record that checks for its update). I'm just not sure why I cannot get a warning when the answer has completed.

I have these GLOBAL (at the top of the javascript page) declared variables.

var AjaxEnginePage; var ClientInfoPage; var XMLHTTP; AjaxEnginePage = "AjaxEngine.aspx"; ClientInfoPage="getClientInfo.aspx"; 

Create a connection.

  //Creating and setting the instance of appropriate XMLHTTP Request object to a "XmlHttp" variable function CreateXMLHTTP() { try { XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP"); } catch(oc) { XMLHTTP = null; } } //Creating object in Mozilla and Safari if(!XMLHTTP && typeof XMLHttpRequest != "undefined") { XMLHTTP = new XMLHttpRequest(); } } 

Link Binding:

 function btnUpdateMe_OnClick() { var me = encodeURIComponent(document.getElementById("MeTextBox").value); // construct the URL var requestUrl = AjaxEnginePage + "?Action=UpdateMe&Me=" + me; CreateXMLHTTP(); // If browser supports XMLHTTPRequest object if(XMLHTTP) { //Setting the event handler for the response XMLHTTP.onreadystatechange = handleStateChange(me); //Initializes the request object with GET (METHOD of posting), //Request URL and sets the request as asynchronous. XMLHTTP.open("get", requestUrl, true); //Sends the request to server XMLHTTP.send(null); } 

Change handle status

  function handleStateChange(me) { switch (XMLHTTP.readyState) { case 0: // UNINITIALIZED case 1: // LOADING case 2: // LOADED case 3: // INTERACTIVE break; case 4: // COMPLETED alert("Success"); break; default: alert("error"); } 

I can provide more code if necessary. :( thanks.

+6
source share
1 answer

Edit:

 XMLHTTP.onreadystatechange = handleStateChange(me); 

in

 XMLHTTP.onreadystatechange = function() {handleStateChange(me);}; 

You set onreadystatechange to the result of the function call, not to the function.

+11
source

Source: https://habr.com/ru/post/944258/


All Articles