Loading...

August 5, 2009

'Invalid label' when using jQuery.getJSON() method

I was working on a small project and I wanted to test some Javascript code that read data formatted as JSON. I setup a small HTML file, sample.html, with a reference to the jQuery library and invoke $.getJSON("http://localhost/example.json", function(data) { alert(data); });. In Firefox I opened http://localhost/sample.html and suspected to see some data. But instead I got an Invalid label in Firebug. What happened? After reading this Stackoverflow answer I knew why. I invoked the $.getJSON() method with a URL starting with http://, and jQuery automatically assumes the request is a JSONP request. But in this case that is not true. Replacing the URL with the relative example.json is the solution.