GM_xmlhttpRequest responseText在Firefox中为空,但在Chrome中是完美的

GM_xmlhttpRequest responseText empty in Firefox but perfect in Chrome

本文关键字:但在 Chrome 完美 xmlhttpRequest responseText Firefox GM      更新时间:2023-09-26

我正在尝试在Firefox中为Chrome和Greasemonkey编写用户脚本。

我使用GM_xmlhttpRequest,因为它应该在这两个平台上都能工作。请求代码似乎在两种浏览器中都有效,但在Firefox中,responseText是空的,而在Chrome中,我得到了预期的响应。

用户脚本代码:

// ==UserScript==  
// @include        *.website.org/Forum/Read.aspx?*  
// ==/UserScript==
        getstr = "thread="+thread+"&day="+getday;
    GM_xmlhttpRequest({
        method: "POST",
        url: "http://www.other.org/js/gm/get.php",
        data: getstr,
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            "Content-type":"charset=utf-8"
        },
        onload: function(response) {
                        alert(response.responseText);
        }
    });

"other.org"网站上的php脚本:

    $json = json_encode($array);
    echo $json;

userscript使用JSON.parse()处理响应,但这在这里并不重要。

在chrome中,这非常有效,但firefox中的responseText是空的。

我读到过,这可能与同源政策有关。但我不明白这可能是怎么回事,也不知道我该怎么解决。我们非常欢迎所有的帮助!

对象不能有多个名称相同的属性。将字符集标头值放入第一个Content-Type中。

此外,请尝试添加更多标头,特别是Content-Length。最安全的方法是检查Firefox在正常POST请求中发送的标题(当您手动提交表单时),并将其全部复制。

相关文章: