我想得到一个 GM_xmlhttpRequest
调用同步行为,但我无法让它像我期望的那样工作:
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。
最佳答案
刚刚在谷歌偶然发现了这个话题。
同步 GM_xmlhttpRequest 返回结果而不是在 onload-callback 中执行。
所以这是正确的:
var details = GM_xmlhttpRequest({
method:"GET",
url:"http://site.com/sample/url",
synchronous: true
});
a = details.responseText;
您在开始时创建 var "a",永远不要填充它并返回它。因此,它是未定义的。
关于synchronization - 同步 GM_xmlhttpRequest 异步操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8778267/