ajax JavaScript XMLHttpRequest.onreadystatechange

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16369395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:00:53  来源:igfitidea点击:

JavaScript XMLHttpRequest.onreadystatechange

javascriptajaxxmlhttprequest

提问by Kulingar

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

我正在尝试做一些 AJAX 并且需要知道为什么这段代码没有触发完成或错误警报。我在 Mozilla Firefox 20.0.1

PLEASE NOTE

请注意

This code ISupdating 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.

此代码IS更新数据库(我有一个SELECT语句读验证它的更新的确切记录)我只是不确定为什么当响应已经完成,我不能得到一个警报。

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

我有这些 GLOBAL (在 javascript 页面的顶部)声明的变量。

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.

如果需要,我可以提供更多代码。:( 谢谢。

回答by Barmar

Change:

改变:

XMLHTTP.onreadystatechange = handleStateChange(me);

to:

到:

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

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

您设置onreadystatechange的是调用函数的结果,而不是函数。