尽管我在凌晨2点钟爱奥秘,但我认为最好问一下。

我正在使用onblur事件传递“ this”(例如this = input.password)。由于某种原因
handleServerResponse不执行任何操作,除非我添加一行,看一下:

正常的ajax功能:

function ajaxFunction(obj)
{
    var button = document.getElementById("submit");
    button.disabled = true;
    button.setAttribute("class", "test");
    var getdate = new Date();  //Used to prevent caching during ajax call
    xmlhttp.onreadystatechange  = handleServerResponse(obj);

    if(xmlhttp)
    {
    //var input = document.forms["signup_form"].getElementsByTagName("input");
    xmlhttp.open("POST","register_ajax.php",true);

    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    xmlhttp.send(obj.name +"="+ obj.value);
    };
}


handleServerResponse-不起作用

function handleServerResponse(obj)
{
    if (xmlhttp.readyState == 4)
    {
        if(xmlhttp.status == 200)
    {
            obj.value=xmlhttp.responseText; //Update the HTML Form element
    }
    else
    {
        alert("Error during AJAX call. Please try again");
    }
    }
}


handleServerResponse-工作

function handleServerResponse(obj)
{
    alert(xmlhttp.responseText);
    if (xmlhttp.readyState == 4)
    {
        if(xmlhttp.status == 200)
    {
            obj.value=xmlhttp.responseText; //Update the HTML Form element
    }
    else
    {
        alert("Error during AJAX call. Please try again");
    }
    }
}

最佳答案

在这条线上

xmlhttp.onreadystatechange  = handleServerResponse(obj);


您正在调用handleServerResponse而不是设置on readystate更改处理程序。您必须将一个功能分配给xmlhttp.onreadystatechange您正在做的是分配handleServerResponse(obj)的返回值。尝试

xmlhttp.onreadystatechange  = function(){
                                  handleServerResponse(obj);
                              };


同样,第二个成功的原因是因为警报阻止了执行,并且在进行检查之前ajax调用已完成。

关于javascript - Javascript Ajax仅在添加虚拟行时唤醒,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15305468/

10-12 16:08