Question

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");
});
Was it helpful?

Solution

The function is $.getJSON and not $.getJson

OTHER TIPS

$.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

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.)

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

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

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".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top