0

I'm using $.getJSON to load a JSON file. The file appears to load successfully (I can see its contents in Firebug below a heading that reads "GET http://siteinfo/data/library.json 304 not modified"). However if I try to use console.log or alert inside the success function it doesn't work):

$.getJSON('data/library.json', function(data){
    alert('HERE');
    console.log(data);
    $.each(data.library, function(k,v){
        console.log(k + "/" + v);

    });
});

None of the alerts or console.logs are working. What could I be doing wrong?

cesarcarlos
  • 1,271
  • 1
  • 13
  • 33

2 Answers2

0

I think it's a problem with caching, you could try doing your request like this, turning caching off

 $.ajax({
      dataType: "json",
      type: "get",
      url: 'data/library.json',
      cache:false,
      success: function(data){
        alert('HERE');
        console.log(data);
        $.each(data.library, function(k,v){
        console.log(k + "/" + v);
        });
      }
    });
George
  • 6,630
  • 2
  • 29
  • 36
0

You are getting 304 status code. That's why response not coming in success function its going to error function because error is a function to be called if the request fails.

There is something wrong with server side. Read Why am I getting "(304) Not Modified" error on some links when using HttpWebRequest? for more details.

Add this error handler in ajax request. You can handle error in ajax. This is following function equivalent to your getJSON

$.ajax({
    type: "get", 
    url: "data/library.json",
    datatype: "json"
    success: function (data, text) {
        alert('HERE');
        console.log(data);
        $.each(data.library, function(k,v){
            console.log(k + "/" + v);

        });
    },
   //add this error handler you'll get alert
    error: function (request, status, error) {
        alert(request.responseText);
    }
});
Community
  • 1
  • 1
Manwal
  • 23,450
  • 12
  • 63
  • 93