Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅Ajax返回readyState==4值_Javascript_Ajax_Xmlhttprequest - Fatal编程技术网

Javascript 仅Ajax返回readyState==4值

Javascript 仅Ajax返回readyState==4值,javascript,ajax,xmlhttprequest,Javascript,Ajax,Xmlhttprequest,好的,我到处都找过了。我删除了几个变量声明,因为我可以确保XMLHttpRequest正常工作 function submit_edit_form() { // id and title are already declared var x = ajax_edit_form_save(id, 'title', title); alert(x); } function ajax_edit_form_save(id, property, new_value) { i

好的,我到处都找过了。我删除了几个变量声明,因为我可以确保XMLHttpRequest正常工作

function submit_edit_form()
{
    // id and title are already declared
    var x = ajax_edit_form_save(id, 'title', title);
    alert(x);
}
function ajax_edit_form_save(id, property, new_value)
{

    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // screw IE5 & IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
       if (xmlhttp.readyState == 4 && xmlhttp.responseText != '')
       {    
            return xmlhttp.responseText;
       }
    }

    // myURL is already defined. I'm not troubleshooting this part, I know it's working
    xmlhttp.open("GET", myURL, true);
    xmlhttp.send();
}

因此,当我调用submit\u edit\u form(),它调用ajax\u edit\u form\u save(),我会收到一个警报“undefined”。我知道问题是ajax\u edit\u form\u save()在readyState 1上返回undefined。我挠头是因为我只有在readyState==4时才能返回。我怎样才能延迟返回值,以便x获得实际的responseText?

您的函数甚至在ajax调用完成之前就返回了,因为它是异步的。“onreadystatechange”中的return语句将不起任何作用,因为该值返回给方法“onreadystatechange”的调用方,该方法是XMLHttpRequest obejct,而不是您的代码

您应该将回调函数传递给您的
ajax\u edit\u form\u save
,该函数在readystate为4时调用

见下文:

function ajax_edit_form_save(id, property, new_value, funCallback) // ==> callback function
{

    if (window.XMLHttpRequest)
    {
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // screw IE5 & IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function()
    {
       if (xmlhttp.readyState == 4 && xmlhttp.responseText != '')
       {    
            callback( xmlhttp.responseText ); // =============> callback function called
       }
    }

    // myURL is already defined. I'm not troubleshooting this part, I know it's working
    xmlhttp.open("GET", myURL, true);
    xmlhttp.send();
}
回拨功能可以是:

function handleReponse(resp) {
  // do something with resp
}

ajax_edit_form_save("myID", "myProperty",  "new value", handleResponse);

你从哪一个文件里得到的(x)?我想,你忘了提到URL值。

老兄,我想我爱你。我完全没有想到这一点。我正要去检查一下,我会给你支票的。这不是问题所在。我知道请求运行正常。我只需要处理返回值。