如何让 XHR.onreadystatechange 返回其结果?

发布于 2024-10-24 07:43:13 字数 672 浏览 3 评论 0原文

我是 JavaScript 编程新手。我现在正在开发我的 Google Chrome 扩展程序。这是不起作用的代码... :P

我希望 getURLInfo 函数返回其 JSON 对象,并希望将其放入 resp 中。有人可以修复我的代码以使其正常工作吗?

function getURLInfo(url)
{
    var xhr = new XMLHttpRequest();
    xhr.open
        (
            "GET",
            "http://RESTfulAPI/info.json?url="
                + escape(url),
            true
        );
    xhr.send();
    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == 4)
        {
            return JSON.parse(xhr.responseText);
        }
    }
}
var resp = getURLInfo("http://example.com/") // resp always returns undefined...

提前致谢。

I'm new to JavaScript programming. I'm now working on my Google Chrome Extension. This is the code that doesn't work... :P

I want getURLInfo function to return its JSON object, and want to put it into resp. Could someone please fix my code to get it work?

function getURLInfo(url)
{
    var xhr = new XMLHttpRequest();
    xhr.open
        (
            "GET",
            "http://RESTfulAPI/info.json?url="
                + escape(url),
            true
        );
    xhr.send();
    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == 4)
        {
            return JSON.parse(xhr.responseText);
        }
    }
}
var resp = getURLInfo("http://example.com/") // resp always returns undefined...

Thanks in advance.

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

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

发布评论

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

评论(3

与往事干杯 2024-10-31 07:43:13

您正在此处处理异步函数调用。结果在到达时处理,而不是在函数完成运行时处理。

这就是回调函数的用途。当结果可用时它们被调用。

function get(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            // defensive check
            if (typeof callback === "function") {
                // apply() sets the meaning of "this" in the callback
                callback.apply(xhr);
            }
        }
    };
    xhr.send();
}
// ----------------------------------------------------------------------------


var param = "http://example.com/";                  /* do NOT use escape() */
var finalUrl = "http://RESTfulAPI/info.json?url=" + encodeURIComponent(param);

// get() completes immediately...
get(finalUrl,
    // ...however, this callback is invoked AFTER the response arrives
    function () {
        // "this" is the XHR object here!
        var resp  = JSON.parse(this.responseText);

        // now do something with resp
        alert(resp);
    }
);

注意:

  • escape() 已被永远弃用。不要使用它,它无法正常工作。使用encodeURIComponent()
  • 您可以通过将 open()async 参数设置为 来使 send() 调用同步>假。这会导致您的 UI 在请求运行时冻结,而您不希望出现这种情况。
  • 有许多库旨在使 Ajax 请求变得简单且通用。我建议使用其中之一。

You are dealing with an asynchronous function call here. Results are handled when they arrive, not when the function finishes running.

That's what callback functions are for. They are invoked when a result is available.

function get(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            // defensive check
            if (typeof callback === "function") {
                // apply() sets the meaning of "this" in the callback
                callback.apply(xhr);
            }
        }
    };
    xhr.send();
}
// ----------------------------------------------------------------------------


var param = "http://example.com/";                  /* do NOT use escape() */
var finalUrl = "http://RESTfulAPI/info.json?url=" + encodeURIComponent(param);

// get() completes immediately...
get(finalUrl,
    // ...however, this callback is invoked AFTER the response arrives
    function () {
        // "this" is the XHR object here!
        var resp  = JSON.parse(this.responseText);

        // now do something with resp
        alert(resp);
    }
);

Notes:

  • escape() has been deprecated since forever. Don not use it, it does not work correctly. Use encodeURIComponent().
  • You could make the send() call synchronous, by setting the async parameter of open() to false. This would result in your UI freezing while the request runs, and you don't want that.
  • There are many libraries that have been designed to make Ajax requests easy and versatile. I suggest using one of them.
始于初秋 2024-10-31 07:43:13

对于异步 XHR 调用,您根本无法执行此操作。你不能让 JavaScript “等待”来自服务器的 HTTP 响应;您所能做的就是向运行时系统传递一个要调用的函数(您的处理程序),它会调用它。然而,该调用将在设置 XHR 的代码完成后很长时间发生。

然而,一切并没有丢失,因为该处理函数可以执行任何事情。无论您想对返回值执行什么操作,都可以在处理程序内部(或从处理程序内部调用的其他函数)执行。

因此,在您的示例中,您将更改如下内容:

    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == 4)
        {
            var resp = JSON.parse(xhr.responseText);
            //
            // ... whatever you need to do with "resp" ...
            //
        }
    }
}

You can't do it at all for asynchronous XHR calls. You cannot make JavaScript "wait" for the HTTP response from the server; all you can do is hand the runtime system a function to call (your handler), and it will call it. However, that call will come a long time after the code that set up the XHR has finished.

All is not lost, however, as that handler function can do anything. Whatever it is that you wanted to do with a return value you can do inside the handler (or from other functions called from inside the handler).

Thus in your example, you'd change things like this:

    xhr.onreadystatechange = function()
    {
        if (xhr.readyState == 4)
        {
            var resp = JSON.parse(xhr.responseText);
            //
            // ... whatever you need to do with "resp" ...
            //
        }
    }
}
撩心不撩汉 2024-10-31 07:43:13

对于谈论帖子的小编辑: https://stackoverflow.com/a/5362513/4766489

...
if (typeof callback == "function") {
     //var resp  = xhr.responseText;
     var resp  = JSON.parse(xhr.responseText);
     callback(resp);
}
...

当你打电话时

...
function(data) {
    alert(data);
    /* now do something with resp */
}
...

For small edit talking about post: https://stackoverflow.com/a/5362513/4766489

...
if (typeof callback == "function") {
     //var resp  = xhr.responseText;
     var resp  = JSON.parse(xhr.responseText);
     callback(resp);
}
...

And when you call

...
function(data) {
    alert(data);
    /* now do something with resp */
}
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文