简体   繁体   中英

jquery getJSON function not running

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?

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);
        });
      }
    });

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);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM