Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Synchronization 同步GM_xmlhttpRequest异步运行?_Synchronization_Greasemonkey_Tampermonkey_Gm Xmlhttprequest - Fatal编程技术网

Synchronization 同步GM_xmlhttpRequest异步运行?

Synchronization 同步GM_xmlhttpRequest异步运行?,synchronization,greasemonkey,tampermonkey,gm-xmlhttprequest,Synchronization,Greasemonkey,Tampermonkey,Gm Xmlhttprequest,我试图让一个GM\u xmlhttpRequest调用同步运行,但我无法让它像我预期的那样工作: function myFunction (arg) { var a; GM_xmlhttpRequest ( { method: "GET", url: "http://example.com/sample/url", synchronous: true, onload:

我试图让一个
GM\u 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回调中执行

所以这是正确的:

var details = GM_xmlhttpRequest({
  method:"GET",
  url:"http://site.com/sample/url",
  synchronous: true
});
a = details.responseText;

您在开始时创建了var“a”,从不填充它并返回它。因此,它是未定义的。

刚刚在谷歌上偶然发现了这个话题

同步GM_xmlhttpRequest返回结果,而不是在onload回调中执行

所以这是正确的:

var details = GM_xmlhttpRequest({
  method:"GET",
  url:"http://site.com/sample/url",
  synchronous: true
});
a = details.responseText;

您在开始时创建了var“a”,从不填充它并返回它。因此,它是未定义的。

是的。。。我不得不重新构造代码,因为这个“bug”不使用同步请求。使用如图所示的异步方法,或等等。。。我不得不重新构造代码,因为这个“bug”不使用同步请求。使用异步方法,如图所示,或等。