1

I am using the following get getJSON

$.getJSON("js/production-data.json").done(function(response) {
    console.log(response);
    console.log('hello');
  });

I can see in Firebug that the data is being retrieved, but there is no console.log for the response which would be the entire data. I can't even see hello either.

L84
  • 45,514
  • 58
  • 177
  • 257
ngplayground
  • 20,365
  • 36
  • 94
  • 173
  • 2
    are you getting a 200 response? remember that if you don't expect a 2XX response you should also define a `fail()` callback – Luis Masuelli Jun 10 '14 at 16:19
  • 1
    Are you sure the request is completing successfully? `.done()` only runs on success. `.fail()` or `.always()` would catch a non-200 response. – Dave Ward Jun 10 '14 at 16:20
  • 2
    @JLRishe sure it does – Nano Jun 10 '14 at 16:20
  • @JLRishe `console.log()` has always worked for me, but in this instance the above doesn't work in either Chrome or Firefox – ngplayground Jun 10 '14 at 16:21
  • Are you getting any other errors? – Jay Blanchard Jun 10 '14 at 16:21
  • try `alert`ing and see what happens – Luis Masuelli Jun 10 '14 at 16:21
  • @LuisMasuelli I am getting a `200 OK 154ms` response – ngplayground Jun 10 '14 at 16:21
  • NO! Never use `alert()` for trouble-shooting. – Jay Blanchard Jun 10 '14 at 16:21
  • 1
    it's just to see if the code is being reached. is not a production code. – Luis Masuelli Jun 10 '14 at 16:22
  • @Liam were you trying to use getJSON in your problem? – ngplayground Jun 10 '14 at 16:24
  • 1
    It doesn't matter @LuisMasuelli, `alert()` is not a trouble-shooting tool. The OP should use tried and true error checking methods. http://stackoverflow.com/questions/8825384/alert-is-bad-really – Jay Blanchard Jun 10 '14 at 16:24
  • Have you tried adding a `fail()` handler? – JLRishe Jun 10 '14 at 16:24
  • @JLRishe just have and it's silently failing – ngplayground Jun 10 '14 at 16:27
  • This usually means the response isn't valid JSON and jQuery is failing to parse it. [Why does $.ajax call for json data trigger the error callback when http status code is “200 OK”?](http://stackoverflow.com/questions/1846086/why-does-ajax-call-for-json-data-trigger-the-error-callback-when-http-status-c) – Jonathan Lonowski Jun 10 '14 at 16:28
  • when i am using the getJSON method and save the data response to a variable and check the variable in the console, it is incorrect, however when I just use the $.get method it saves the data to the variable as expected. On both methods however, I am unable to console.log anything. – pizzarob Jun 10 '14 at 16:29
  • 1
    I just verified the JSON and now it seems to work. Thanks for the tip @JLRishe to set a `fail()` – ngplayground Jun 10 '14 at 16:30
  • 1
    Does the result include a json content-type and is it valid json if either of these are false it will fail silently. This is why it's always best to use the $.ajax call. – Liam Jun 10 '14 at 16:31
  • @Donald It was actually Dave Ward's tip :) but I'm glad it solved your issue. – JLRishe Jun 10 '14 at 16:34
  • @JayBlanchard I never cared about alert being a troubleshooting tool. I suggested it just to see if he had a problem with a browser plugin or a problem with the call. Get it (for once): It is not about best practices, but about having an alleged problem with console.log. – Luis Masuelli Jun 10 '14 at 17:08

2 Answers2

0

Try specifying the callback inline, like:

var jqxhr = $.getJSON( "example.json", function() {
  console.log( "success" );
});

As another step, add an always handler, rather than only done.

dwerner
  • 6,462
  • 4
  • 30
  • 44
0
$.getJSON("data.json", function(d) {

  }).done(function(d) {
    console.log("success");
  }).fail(function(d) {
    console.log("error");
  }).always(function(d) {
    console.log("complete");
  });

Also get the JSON verified at JSONLint

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
ngplayground
  • 20,365
  • 36
  • 94
  • 173