簡體   English   中英

jQuery getJSON函數未運行

[英]jquery getJSON function not running

我正在使用$ .getJSON加載JSON文件。 該文件似乎已成功加載(我可以在Firebug中的標題為“ 未修改 GET http://siteinfo/data/library.json 304”的標題下看到其內容)。 但是,如果我嘗試使用console.log或成功功能內部的警報,則它不起作用):

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

    });
});

沒有任何警報或console.logs在工作。 我可能做錯了什么?

我認為這是緩存問題,您可以嘗試執行這樣的請求,然后關閉緩存

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

您正在獲取304狀態代碼。 這就是為什么響應不會進入success函數而轉到error函數的原因,因為error是請求失敗時要調用的函數。

服務器端出了點問題。 閱讀為什么使用HttpWebRequest時某些鏈接上出現“(304)Not Modified”錯誤? 更多細節。

在ajax請求中添加此錯誤處理程序。 您可以處理ajax中的錯誤。 這是等效於您的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);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM