同步GM_xmlhttpRequest异步执行吗? [英] Synchronous GM_xmlhttpRequest acting asynchronously?
问题描述
我正在尝试获取GM_xmlhttpRequest
调用以使其行为同步,但是我无法使其按预期运行:
I'm trying to get a GM_xmlhttpRequest
call to behave synchronously, but I can't get it to work like I expect:
function myFunction (arg) {
var a;
GM_xmlhttpRequest ( {
method: "GET",
url: "http://example.com/sample/url",
synchronous: true,
onload: function (details) {
a = details.responseText;
}
} );
return a;
}
b = myFunction ();
alert (b);
在这里,我从没拿到b
的任何东西;它是未定义的.我在这里缺少一些步骤吗?
我正在使用Greasemonkey的v0.9.13和Firefox的v9.0.1.
I never get anything back for b
here; it's undefined. Is there some step that I'm missing here?
I'm using v0.9.13 of Greasemonkey, and v9.0.1 of Firefox.
推荐答案
在Google中偶然发现了这个主题.
Just stumbled upon this topic in Google.
同步GM_xmlhttpRequest返回结果,而不是在onload-callback中执行它.
Synchronous GM_xmlhttpRequest RETURN the result instead of executing it in the onload-callback.
所以这是正确的:
var details = GM_xmlhttpRequest({
method:"GET",
url:"http://site.com/sample/url",
synchronous: true
});
a = details.responseText;
您在开头创建了变量"a",切勿填充并返回它.因此,它是未定义的.
You create the var "a" in the beginning, never fill it and return it. Therefore, it is undefined.
这篇关于同步GM_xmlhttpRequest异步执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!