0

I am calling a JSP by passing parameters which outputs a valid JSON as response, but still the $.getJson callback function is not getting fired. JSP page output is

 { "data": [ [ [ 1258185480000,4.39], 
               [ 1258186020000,4.31],
               [ 1258184940000,4.39],
               [ 1258183560000,4.39]  ] ] }

The URL points to the JSP page

My jquery code is

<script id="source" language="javascript" type="text/javascript">
$(function () {   
  alert("before");
  $.getJson(URL,function(json){
            alert("hello");
          var plot = $.plot($("#placeholder"), json.data, options);
    });

 alert("after");
});

6 Answers 6

11
$.getJSON( URL, function(data) {
  alert("hello");
});

is nothing but a shorthand for ajax call

$.ajax({
  dataType: "json",
  url: URL,
  data: data,
  success: function(data) {
    alert("hello");
  }
});

BUT

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently ... For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes

source: jquery.getjson docs

Sign up to request clarification or add additional context in comments.

1 Comment

It makes sense to me: "if the JSON file contains a syntax error, the request will usually fail silently!"
10

The function is $.getJSON and not $.getJson

4 Comments

I changed to getJSON and tried, still the callback did not get triggered. How to validate json in firebug? I verified in Jsonlint that the response from JSP is a valid JSON
Is your server sending the correct MIME type: application/json or application/javascript?
$.ajax({ type: "GET", url: URL, dataType: "json", success: function(results){ alert("Success!"); }, error: function(XMLHttpRequest, textStatus, errorThrown){ alert(textStatus); alert(errorThrown); } }); I get a parse error when I use $.ajax instead of $.getJSON. The data I get back from JSP is { "data": [ [ [ 1258185480000,4.39], [ 1258186020000,4.31], [ 1258184940000,4.39], [ 1258183560000,4.39] ] ] } I have verified that the JSON is proper in jsonlint
@Santhosh, you need to set the proper Content-Type header in your JSP page. Please verify with FireBug that when you perform the request, the server sends Content-Type: application/json header.
3

$.getJSON will not utilize a callback with out a proper JSON object to process.

2 Comments

A link to jQuery documentation would highly improve your answer (here it is : api.jquery.com/jQuery.getJSON)
This Helped! I am testing in chrome 64.0.3282.167. I was really stuck because chrome was showing the call was made and had all the expected headers and format. Then I saw this comment and thought I should find a linter for json. Sure enough I found jsonlint.com and it shows a failure It took a while to realize I had single quotes. The JSON spec dictates double quotes. With this change $.getJOSN() worked as expected.
2

I just spent about two hours on this. I found another post that discusses the difference between $.getJSON and $.get and how there really isn't any. So I swapped out my getJSON() for get() and it worked.

(Also want to mention that I had also verified everything else was working by logging from the rails action and logging what I could from the javascript outside the callback function.)

2 Comments

I believe the different between $.getJSON and $.get is in the content type (JSON vs XML).
I am not sure about the difference but $.get did return the JSON object. Thanks guys.
1

Also ensure with Firebug that you are getting valid JSON back from the server.

3 Comments

I changed to getJSON and tried, still the callback did not get triggered. How to validate json in firebug? I verified in Jsonlint that the response from JSP is a valid JSON
On the Net tab in Firebug you'll be able to see all of the requests/responses.
$.ajax( { type: "GET", url: URL, dataType: "json", success: function(results){ alert("Success!"); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert(textStatus); alert(errorThrown); } }); I get a parse error when I use $.ajax instead of $.getJSON. The data I get back from JSP is { "data": [ [ [ 1258185480000,4.39], [ 1258186020000,4.31], [ 1258184940000,4.39], [ 1258183560000,4.39] ] ] } I have verified that the JSON is proper in jsonlint. How to find what the parse error is ?
1

For jQuery 3.4.1:

$.getJSON("test.json", function (json) {
    console.log('Got JSON');
    console.log(json);

})
.fail(function (jqxhr, textStatus, error) {
    var err = textStatus + ", " + error;
    alert("There has been an error. If the problem persists contact the customer service");
})
.always(function () {
    console.log("complete");
});

If you believe that the JSON is ok and you are using chrome try "Empty Cache and Hard Reload".

1 Comment

Great for debugging, although much more useful with alert(err) rather than the fixed error message.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.