tags:

views:

58

answers:

4

Hi

I have the following jquery in my code$j.getJSON("http://localhost:8080/WWTestHarnessWEB/ReadersToolkitFinalController.htm?jsoncallback=?", {'uID': 1}, function(data){alert(data);});

The json that i am returning looks like the following ({"positiveCount":"0","negativeCount":"999"})

But my alert is never firing.

Any idea what i need to do to ensure that this will work?

regards Damien

+1  A: 

try this:

$j.getJSON(
  "http://localhost:8080/WWTestHarnessWEB/ReadersToolkitFinalController.htmjsoncallback=?", 
  {'uID': 1}, 
  function(data, status){
    alert(status);
  });

what does this output?

Rixius
What happened to that URL? Surely initial `?` starts the query string.
T.J. Crowder
+2  A: 

I believe that if you're using a different name for the callback query string parameter than callback (looks like you're using jsoncallback instead), you have to tell jQuery; see the jsonp parameter in the options object on the $.ajax call. This means you'll have to use ajax instead of getJSON, but that's easy enough.

E.g.:

$.ajax({
  url:      "http://localhost:8080/WWTestHarnessWEB/ReadersToolkitFinalController.htm?jsoncallback=?",
  dataType: "json",
  jsonp:    "jsoncallback",
  data:     {'uID': 1},
  success:  function(data){alert(data);}
});

I'm not sure whether you need to (or should) include the "jsoncallback=?" in the URL yourself or let jQuery put it in; a quick experiment should tell you.

T.J. Crowder
+1  A: 

Does your server code actually obey the jsoncallback parameter? If I understand you correctly, you're just returning

({"positiveCount":"0","negativeCount":"999"})

whereas the P in JSONP means you need to return

callback1234({"positiveCount":"0","negativeCount":"999"})

where callback1234 is the value of the jsoncallback parameter. (Although the URL says jsoncallback=?, JQuery interprets that specially, and replaces the ? with a unique generated string.)

Also, what T. J. Crowder said.

Sam Stokes
A: 

Cheers guys for the responses.

It turned out that it was very finicky and by applying bits and pieces of what you guys showed me worked

here now is my jquery code $j.getJSON( "http://localhost:8080/WWTestHarnessWEB/ReadersToolkitFinalController.htm?jsoncallback=?",
{'uID': 1},
function(data, status){ if (status != 'success') { alert("Status is: " + status); } var positiveCount = data.positiveCount;
var negativeCount = data.negativeCount; $j('td[id*="posiiveVoteId"]').html(getCorrectCountString(positiveCount)); $j('td[id*="negativeVoteId"]').html(getCorrectCountString(negativeCount)); });

My json response is as follows `ParamsHeadersPostPutResponseCacheHTMLJSON Response Headersview source Server Apache-Coyote/1.1 Content-Type application/json;charset=ISO-8859-1 Content-Language en-US Content-Length 80 Date Wed, 26 May 2010 16:38:43 GMT Request Headersview source Host localhost:8080 User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 Accept text/javascript, application/javascript, / Accept-Language en-us,en;q=0.5 Accept-Encoding gzip,deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive 115 Connection keep-alive Content-Type application/x-www-form-urlencoded X-Requested-With XMLHttpRequest Referer http://localhost:8080/WWTestHarnessWEB/jsp/ReadersToolkit/Json.jsp Cookie JSESSIONID=93FDA3BD0D2450D7FA4BD9A76C388F8A

jsonp1274891923240({"positiveCount":"0","negativeCount":"999"})`

Thanks again Damien

Damo