views:

69

answers:

2

It looks like people have had issues with Accept headers in the past, but I'm not sure my issue is related. Using jQuery 1.4.2, I'm having trouble getting JSON with getJSON(). I can watch the request / response in Firebug and it looks like the source of the problem is that the resource in question returns different results depending on the Accept header. Even though the docs say it should be set, in Firebug it shows up as "/" -- obviously, I want "application/json". Is this a known bug? Am I supposed to be setting some flag I'm not aware of?

ETA: The request is cross-site, if that matters, but I'm passing a callback=? query parameter so JQuery is (successfully!) treating it as JSONP. The service I'm calling in this particular case supports an accept override query parameter (&accept=application/json), so I got it to work manually, but I still consider the header screwup to be strange and was hoping I'd be able to fix it, so I don't run into this again when dealing with a different service that might not be so forgiving. I don't have an easy way to copy/paste the code from my development environment but here's the gist:

$.getJSON(baseURL + "?item=" + itemNum + "&callback=?", function(data){
  console.log(data);
}

As you can see, this is not exactly complex, and should (I'm 99% sure...) result in an XHR being sent with an Accept header of application/json. Like I said, that's not happening, per Firebug's Net console. If it matters, this is in Firefox 3.6.8.

ETA Again: For anybody still reading this, yes, it's still happening, and no, I have no idea why. Like I said, simple getJSON() call, really basic syntax, cross site, treated as JSONP because it includes a callback query parameter. Still open to suggestions!

A: 

Without seeing your code (which might point us to an obvious solution,) can you try using the standard Ajax function and see if you get different results?

$.ajax({
  url: '/what.eva',
  dataType: 'json',
  data: '{}',
  success: callbackFunc
});

function callbackFunc(result) {
   alert(result);
} 
Fosco
I've tried what you suggested, replacing my getJSON call (details now listed in the main question) with a $.ajax invocation, and the results are exactly the same. I figure this has to be a known bug, or maybe it's just that my browser is weird, but this definitely runs counter to expected behavior.
Coderer
+1  A: 

This is a bug which has been closed on the jquery website.

http://dev.jquery.it/ticket/6551

There does not appear to be a fix for this yet.

Timothy Strimple
Wow, so somebody else has the issue, but they can't reproduce so they closed it? I'll see if I can contribute to it, somehow...
Coderer