2

I'm trying to do a userscript for Chrome and Greasemonkey in Firefox.

I'm using the GM_xmlhttpRequest since it supposed to work in both platforms. The request code seems to work in both browsers, but in Firefox the responseText is empty in contrast to Chrome where I get the expected response.

The userscriptcode:

// ==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);
        }
    });

The php-script on the "other.org" site:

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

The userscript handles the response with a JSON.parse(), but that is not important here.

In chrome, this works perfectly, but the responseText is empty in firefox.

I have read about that this might be something to do with the same-origin policies. But I don't understand how this might be and how I can fix it. All help is very welcome!

Juniperus
  • 77
  • 1
  • 6
  • It actually doesn't seem to post the data at all. The onload function is activated anyway. The empty responseText may be because the php-script doesn't get any data to process. – Juniperus Jan 15 '12 at 15:20
  • Objects can't have multiple properties with identical names. Put the charset into the first `Content-Type`. Also, try adding more headers, `Content-Length`, specifically. The safest bet is to inspect what headers Firefox sends on normal POST-requests (when you submit the form manually) and copy them all. – katspaugh Jan 15 '12 at 15:58
  • 1
    Juniperus, no problem. What exactly solved your question, eliminating duplicate header properties? – katspaugh Jan 15 '12 at 16:37
  • Yes. I added the charset to the first content type instead of having two. Thanks again! – Juniperus Jan 20 '12 at 00:03

1 Answers1

3

Objects can't have multiple properties with identical names. Put the charset header value into the first Content-Type.

Also, try adding more headers, Content-Length, specifically. The safest bet is to inspect what headers Firefox sends on normal POST-requests (when you submit the form manually) and copy them all.

katspaugh
  • 15,752
  • 9
  • 61
  • 97