【问题标题】:Synchronous GM_xmlhttpRequest acting asynchronously?同步 GM_xmlhttpRequest 异步执行?
【发布时间】:2012-02-05 09:19:30
【问题描述】:

我正在尝试让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 在这里我再也没有得到任何回报;它是未定义的。我在这里缺少一些步骤吗?
我正在使用 v0.9.13 的 Greasemonkey 和 v9.0.1 的 Firefox。

【问题讨论】:

  • 是的......由于这个“错误”,我不得不重组我的代码
  • 不要使用同步请求。使用如herehere 等所示的异步方法。

标签: synchronization greasemonkey tampermonkey gm-xmlhttprequest


【解决方案1】:

刚刚在 Google 中偶然发现了这个主题。

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

所以这是对的:

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

您在开始时创建 var "a",永远不要填充它并返回它。因此,它是未定义的。

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2011-08-22
    相关资源
    最近更新 更多