最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - JavaScript XMLHttpRequest.onreadystatechange - Stack Overflow

programmeradmin10浏览0评论

I'm attempting to do some AJAX and need to know why this code isn't firing a completed or error alert. I'm in Mozilla Firefox 20.0.1

PLEASE NOTE

This code IS updating the database (I have a select statement reading the exact record verifying it's updating) I'm just unsure as to why I can't get an alert when the response 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";

Creating the 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();
}
}

Tying the connection:

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);     
}

Handle State Change

 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 needed. :( thanks.

I'm attempting to do some AJAX and need to know why this code isn't firing a completed or error alert. I'm in Mozilla Firefox 20.0.1

PLEASE NOTE

This code IS updating the database (I have a select statement reading the exact record verifying it's updating) I'm just unsure as to why I can't get an alert when the response 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";

Creating the 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();
}
}

Tying the connection:

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);     
}

Handle State Change

 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 needed. :( thanks.

Share Improve this question asked May 4, 2013 at 0:30 KulingarKulingar 9613 gold badges17 silver badges30 bronze badges 3
  • 1 With this line, XMLHTTP.onreadystatechange = handleStateChange(me);, you're immediately calling handleStateChange and returning its return value to onreadystatechange (it returns nothing, so the return value is undefined). onreadystatechange expects a reference to a function. Also, you pass me, but you don't use it in the handleStateChange function. – Ian Commented May 4, 2013 at 0:33
  • I realize and apologize for not using me, I meant to remove that. – Kulingar Commented May 4, 2013 at 1:26
  • No need to apologize, I just thought I'd point it out. Since it isnt used in the function, it is easier to fix your problem. If it was needed, youd have to add a little more – Ian Commented May 4, 2013 at 3:08
Add a comment  | 

1 Answer 1

Reset to default 13

Change:

XMLHTTP.onreadystatechange = handleStateChange(me);

to:

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

You're setting onreadystatechange to the result of calling the function, not to the function.

发布评论

评论列表(0)

  1. 暂无评论