无法从 JavaScript 函数返回对象的值

发布于 2024-11-17 00:13:26 字数 861 浏览 4 评论 0原文

我有一个函数尝试通过以下方式从调用函数捕获返回值:

var select = xhrRetrieve(projID);

这是 xhrRetrieve 函数的示例:

function xhrRetrieve(projID) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            if(xhr.status == 200) {
                var obj = $.parseJSON(xhr.responseText);

                return obj.select.toString();
            }
        }
    }

    var url = "ajax.cgi";
    var data = "action=retrieve-opp&proj-id=" + projID;

    xhr.open("POST",url);
    xhr.setRequestHeader("Content-Type","application/x-www-urlencoded");
    xhr.send(data);

}

我将 jQuery 与直接 JavaScript 结合使用。每当我尝试使用以下方法获取 obj.select 的值时:

var select = xhrRetrieve(projID);

Select 总是返回 undefined

我做错了什么?

I have a function which attempts to capture a return value from a calling function in the following manner:

var select = xhrRetrieve(projID);

Here is an example of the xhrRetrieve function:

function xhrRetrieve(projID) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            if(xhr.status == 200) {
                var obj = $.parseJSON(xhr.responseText);

                return obj.select.toString();
            }
        }
    }

    var url = "ajax.cgi";
    var data = "action=retrieve-opp&proj-id=" + projID;

    xhr.open("POST",url);
    xhr.setRequestHeader("Content-Type","application/x-www-urlencoded");
    xhr.send(data);

}

I am using jQuery in conjunction with straight JavaScript. Whenever I attempt to get the value of obj.select using:

var select = xhrRetrieve(projID);

Select always comes back undefined.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独闯女儿国 2024-11-24 00:13:26
  1. 该函数不返回任何内容
  2. 当您调用函数时,(当前不存在的)返回值将被分配给 select。与此同时,您的 ajax 请求正在被触发,这需要时间才能完成;在 ajax 请求完成(并成功)之前,回调函数不会被调用。

这应该有效:

function doStuffWithTheAjaxResponse(select) {
   // do stuff
}

function xhrRetrieve(projID) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            if(xhr.status == 200) {
                var obj = $.parseJSON(xhr.responseText);

                doStuffWithTheAjaxResponse(obj.select.toString());
            }
        }
    }

    var url = "ajax.cgi";
    var data = "action=retrieve-opp&proj-id=" + projID;

    xhr.open("POST",url);
    xhr.setRequestHeader("Content-Type","application/x-www-urlencoded");
    xhr.send(data);

}
  1. The function doesn't return anything
  2. The moment you call your function, the (not currently present) return value is being assigned to select. At the same moment, your ajax request is being fired, which takes time to complete; the callback function will not be called until the ajax request has completed (and succeeded).

This should work:

function doStuffWithTheAjaxResponse(select) {
   // do stuff
}

function xhrRetrieve(projID) {
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4) {
            if(xhr.status == 200) {
                var obj = $.parseJSON(xhr.responseText);

                doStuffWithTheAjaxResponse(obj.select.toString());
            }
        }
    }

    var url = "ajax.cgi";
    var data = "action=retrieve-opp&proj-id=" + projID;

    xhr.open("POST",url);
    xhr.setRequestHeader("Content-Type","application/x-www-urlencoded");
    xhr.send(data);

}
淡淡绿茶香 2024-11-24 00:13:26

由于请求是异步的,该函数将在 onreadestatechange 中的代码触发之前返回。您可以切换到同步并在函数返回之前获取值:

function xhrRetrieve(projID) {
    var returnVal;
    var xhr = new XMLHttpRequest();

    var url = "ajax.cgi";
    var data = "action=retrieve-opp&proj-id=" + projID;

    //3rd param is false to switch to synchronous
    xhr.open("POST",url, false);
    xhr.setRequestHeader("Content-Type","application/x-www-urlencoded");
    xhr.send(data);
    if(xhr.readyState == 4) {
        if(xhr.status == 200) {
            var obj = $.parseJSON(xhr.responseText);
            return obj.select.toString();
        }
    }
}

Since the request is asynchronous the function will return before your code in onreadestatechange fires. You can switch to synchronous and get the value before the function returns:

function xhrRetrieve(projID) {
    var returnVal;
    var xhr = new XMLHttpRequest();

    var url = "ajax.cgi";
    var data = "action=retrieve-opp&proj-id=" + projID;

    //3rd param is false to switch to synchronous
    xhr.open("POST",url, false);
    xhr.setRequestHeader("Content-Type","application/x-www-urlencoded");
    xhr.send(data);
    if(xhr.readyState == 4) {
        if(xhr.status == 200) {
            var obj = $.parseJSON(xhr.responseText);
            return obj.select.toString();
        }
    }
}
一指流沙 2024-11-24 00:13:26

函数 xhrRetrieve 没有返回值。您预计会发生什么?

The function xhrRetrieve doesn't have a return value. What do you expect to happen?

不醒的梦 2024-11-24 00:13:26

你在那里有两个功能。内部函数返回一个值,但外部函数不返回值。内部函数是一个事件处理程序,因此返回值不会去任何地方。您的 XMLHttpRequest 是异步的,因此您不会立即获得返回值。有关更详细的说明,请参阅这篇文章: xmlHttpRequest 中的参数“true”。 open()方法

You have two functions there. The inner function returns a value, but not the outer one. The inner function is an event handler so the return value doesn't go anywhere. Your XMLHttpRequest is asynchronous, so you won't get a return value right away. See this post for a more detailed explanation: parameter "true" in xmlHttpRequest .open() method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文