Javascript Ajax responseText返回为未定义

Javascript Ajax responseText返回为未定义,javascript,ajax,Javascript,Ajax,我对这段代码有问题;返回值返回为“未定义”。有什么问题吗 var fx = null; xmlhttp.open("GET", URL ,false); xmlhttp.onreadystatechange=function() { alert("enter func"); if (xmlhttp.readyState==4) { if (xmlhttp.status == 200)

我对这段代码有问题;返回值返回为“未定义”。有什么问题吗

var fx = null;
xmlhttp.open("GET", URL ,false);
xmlhttp.onreadystatechange=function() 
    {
        alert("enter func");            
    if (xmlhttp.readyState==4) 
        {
        if (xmlhttp.status == 200) 
            {                   
                alert(fx);                  
                fx = xmlhttp.responseText;
                return fx;
            }
        else
            {
                alert("Error" + xmlhttp.statusText);
            }
        }
    }
更新的代码:

function getData(callback)
 {      
    xmlhttp.open("GET", URL ,false);
    xmlhttp.onreadystatechange=function() 
        {               
        if (xmlhttp.readyState==4) 
            {
            if (xmlhttp.status == 200) 
                {                   
                    alert(xmlhttp.responseText);
                    cbfunc(xmlhttp.responseText);                   
                }
            else
                {
                    alert("Error" + xmlhttp.statusText);
                }
        }
    }   
        xmlhttp.send(null);
 }
我如何称呼它:

getData( function cbfoo(txt)
         {
            //document.form.autodate.value=txt;
            alert(txt);
            alert(document.form.autodate.value);
         });`

根据您的编辑进行更新

function getData(callback)
 {       
    // you should  move the creation of xmlhttp in here
    // so you can make multiple getData calls if needed 
    // if you keep xmlhttp outside the function the different calls to getData will interfere 
    // with each other

    xmlhttp.open("GET", URL ,false); // false should be true, to make it async
    ...
                {                   
                    alert(xmlhttp.responseText);
                    cbfunc(xmlhttp.responseText); // your function gets passed as the 
                                                  // parameter "callback" but you're 
                                                  // using "cbfunc" here instead of "callback"
 ...
 getData(function cbfoo(txt) // you can omit the function name here
 ...
修复这些问题应该可以使代码正常工作

旧答案

您在同步模式下调用XMLHttpRequest,这意味着它将阻塞脚本,直到请求完成,因为您在阻塞调用之后(即请求完成之后)分配
onreadystatechange
回调,您的代码永远不会得到通知

由于同步模式会阻止脚本,因此也会阻止浏览器的UI,因此不建议使用此模式

您应该(对于99%的情况)使用异步模式并使用回调来处理数据,因为
xmlhttp.open
不会返回
onreadystatechange
回调的返回值,在异步模式下运行时,它只会立即返回
未定义的

现在,一种常见的模式是为请求编写一个包装器,并将一个匿名函数传递给该包装器,该包装器稍后将在请求完成时被调用

function doRequest(url, callback) {
    var xmlhttp = ....; // create a new request here

    xmlhttp.open("GET", url, true); // for async
    xmlhttp.onreadystatechange=function() {     
        if (xmlhttp.readyState==4) {
            if (xmlhttp.status == 200) {

                // pass the response to the callback function
                callback(null, xmlhttp.responseText);

            } else {
                // pass the error to the callback function
                callback(xmlhttp.statusText);
            }
        }
    }
    xmlhttp.send(null);
}
您现在可以执行一个请求并提供一个函数,该函数将在请求完成后立即被调用,在该函数中,您可以对响应执行任何您想执行的操作

doRequest('http://mysite.com/foo', function(err, response) { // pass an anonymous function
    if (err) {
        alert('Error: ' + err);

    } else {
        alert('Response: ' + response);
    } 
});

这是浏览器中常见的编程模型,始终使用异步解决方案,如果阻止脚本,则会阻止整个浏览器。

实际上
xmlhttp.open(“GET”,URL,false)使调用同步,以便永远不会触发onreadystatechange@Hemlock Dang,这就是你一直使用jquery所得到的。。。我要调整我的答案。@Ivo Wetzel Best response不管怎样,我实际上是从我调用的servlet中得到了我需要的值,唯一的问题是它实际上是在onreadystatechange函数之外返回的。很抱歉省略了这条信息。如果我在函数中设置了一个var,并在返回之前用警报检查它,我就得到了我需要的。在那之后,我只得到“undefined”。@RedForeman您的代码中没有“外部返回”,您无法访问
onreadystatechange
回调的返回值。你的作业看起来怎么样?