如何让 XHR.onreadystatechange 返回其结果?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在此处处理异步函数调用。结果在到达时处理,而不是在函数完成运行时处理。
这就是回调函数的用途。当结果可用时它们被调用。
注意:
escape()
已被永远弃用。不要使用它,它无法正常工作。使用encodeURIComponent()
。open()
的async
参数设置为来使
send()
调用同步>假。这会导致您的 UI 在请求运行时冻结,而您不希望出现这种情况。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.
Notes:
escape()
has been deprecated since forever. Don not use it, it does not work correctly. UseencodeURIComponent()
.send()
call synchronous, by setting theasync
parameter ofopen()
tofalse
. This would result in your UI freezing while the request runs, and you don't want that.对于异步 XHR 调用,您根本无法执行此操作。你不能让 JavaScript “等待”来自服务器的 HTTP 响应;您所能做的就是向运行时系统传递一个要调用的函数(您的处理程序),它会调用它。然而,该调用将在设置 XHR 的代码完成后很长时间发生。
然而,一切并没有丢失,因为该处理函数可以执行任何事情。无论您想对返回值执行什么操作,都可以在处理程序内部(或从处理程序内部调用的其他函数)执行。
因此,在您的示例中,您将更改如下内容:
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:
对于谈论帖子的小编辑: https://stackoverflow.com/a/5362513/4766489
当你打电话时
For small edit talking about post: https://stackoverflow.com/a/5362513/4766489
And when you call