JavaScript的XMLHtt prequest.onreadystatechangeXMLHtt、JavaScript、onreadystatechange、prequest

2023-09-10 16:44:55 作者:你是我的风景

我试图做一些AJAX和需要知道为什么code未烧制完成或错误警报。我在Mozilla Firefox 20.0.1

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

这code 是的更新数据库(我有一个select语句读取准确的记录核实它的更新),我只是不确定,为什么我可以'T得到一个警告,当响应完成。

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.

我有这些全球性的(在JavaScript页​​面的顶部)声明的变量。

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

如果需要,我可以提供更多的code。 :(感谢。

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

推荐答案

修改

XMLHTTP.onreadystatechange = handleStateChange(me);

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

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

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