7

I've narrowed my problem area down to the function below. It's part of a userscript I'm writing. It works perfectly in Chrome, but doesn't work at all in Firefox/Greasemonkey. I've tinkered with it all day and have hit a brick wall. The only thing that makes sense is if JSON.parse isn't working right, which would make sense since Chrome is known to handle JSON.parse somewhat differently... but I know the JSON is perfectly formed!

function getTagline() {
    var jsonfile = new XMLHttpRequest();
    jsonfile.open("GET", "http://example.com/somegood.json", true);
    jsonfile.onreadystatechange = function() {
        if (jsonfile.readyState == 4) {
            if (jsonfile.status == 200) {
                var taglines = JSON.parse(jsonfile.responseText);
                var choose = Math.floor(Math.random() * taglines.length);
                var tagline = document.createTextNode(taglines[choose].metais);
                insertTagline(tagline);
            }
        }
    };
    jsonfile.send(null);
}

Any ideas?

gilrain
  • 111
  • 1
  • 7
  • Could you post part of the JSON you are trying to parse? – Grego Sep 24 '11 at 23:13
  • Here's an example: [{"commenturl": ["/107669/Solutionism-is-the-new-Optimism#3934686"], "metais": ["Metafilter: took the day off and sat in the basement in the dark and didn't really respond to anything at all"], "user": ["uncanny hengeman"]},{"commenturl": ["/107126/Lancelot-Link-Secret-Chimp#3905976"], "metais": ["Metafilter: They're not monkeys. They're pedants."], "user": ["Daddy-O"]}, {"commenturl": ["/107129/My-job-is-to-watch-dreams-die#3906426"], "metais": ["MetaFilter: mostly terrible people hiding behind good links"], "user": ["Faint of Butt"]}] – gilrain Sep 24 '11 at 23:20
  • So, it's a list of objects. It should be valid, according to JSON.org. I think... :P – gilrain Sep 24 '11 at 23:21
  • Works fine for me, but have to escape the single quotes in Firebug (\'). http://o7.no/nJ2nJA – Eric Sep 25 '11 at 02:03
  • Does that script reach the JSON.parse line? If so, does that call throw, and if so, what exception? – Boris Zbarsky Sep 25 '11 at 14:51

2 Answers2

1

I was told that JSON is not supported without an extra library, see here the accepted answer. I also tried this

try {
    clientList = JSON.parse(responseText);
} catch (e) {
    alert(e.message);
}

And the message I get is "JSON is undefined". So the answer seems correct.

Community
  • 1
  • 1
Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69
  • Thank you! I'll give that a try tomorrow. [This article](https://developer.mozilla.org/En/Using_native_JSON) made me think it was supported, though... I'll comment again after I've tried it. – gilrain Sep 25 '11 at 00:33
  • Hm, looks like that's not it... switched to using eval for that line, to test. It still works fine in Chrome, using eval, but no go in Firefox. – gilrain Sep 25 '11 at 01:47
1

After some more troubleshooting, it turns out the was a cross-domain XHR issue. It was working in Chrome because, by default, Chrome was allowing the script on all domains. I tweaked the headers so Chrome knew to only allow the proper domains, but Firefox disallows cross-domain on XHR regardless. This was fixed by simply switching to GM_xmlhttpRequest instead, which allows cross-domain in Firefox and, thankfully, which Chrome also supports.

Thanks for the help, folks!

gilrain
  • 111
  • 1
  • 7