GM_xmlhttpRequest responseText在Firefox中为空,但在Chrome中非常完美

时间:2012-01-15 15:00:58

标签: javascript ajax greasemonkey userscripts gm-xmlhttprequest

我正在尝试在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;

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

在chrome中,这可以很好地工作,但是firefox中的responseText是空的。

我已经读到这可能与同源策略有关。但我不明白这是怎么回事以及如何解决它。非常欢迎所有帮助!

1 个答案:

答案 0 :(得分:3)

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

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

相关问题