2017-03-01 176 views
0

我想從外部URL填充json數據的我的選擇框。Ajax未捕獲TypeError:無法讀取未定義的屬性「長度」

我不斷收到

Uncaught TypeError: Cannot read property 'length' of undefined

下面

是我的JSON響應

{"ecoachlabs":{ 
    "status":"201", 
    "msg":"Form data loaded successfully.", 
    "categories":[ 
    {"id":"2","category":"church"},{"id":"3","category":"financial institution"}, 
    {"id":"4","category":"old students association"}, 
    {"id":"1","category":"school"}, 
    {"id":"5","category":"tertiary"} 
    ], 
    "storage":[ 
    {"id":"1","category":"100MB"},{"id":"2","category":"250MB"}, 
    {"id":"3","category":"500MB"},{"id":"4","category":"2GB"}, 
    {"id":"5","category":"3GB"},{"id":"6","category":"5GB"} 
    ] 
    }} 
下面

是我的ajax郵編

$(document).ready(function() { 
    $("#institution_category").click(function() { 

    var formData = { 
     load_request_form_data: "1" 
    }; //Array 

    $.ajax({ 
     url: "http://api.ecoachlabs.com/v1/requests/api.php", 
     type: "POST", 
     data: formData, 
     success: function(data, textStatus, jqXHR) { 
     console.log(data.ecoachlabs.categories); //data - response from server 
     $.each(data.categories, function(i, v) { 
      $('#institution_category').append($('<option value="' + v.id + '">' + v.category + '</option>')); 
     }); 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 

     } 
    }); 
    }); 
}); 

+2

確實'data.categories'存在嗎?在使用'data.ecoachlabs.categories'之前的行中。如果你給'$ .each'一個空或非數組值,你有時可能會得到這個錯誤 –

回答

4

你做一個控制檯日誌onsole.log(data.ecoachlabs.categories);,然後使用data.categories。將data.categories更改爲data.ecoachlabs.categories以正確解析對象,否則您將獲得undefined。請參見下面的代碼片段工作請:

var formData = { 
 
    load_request_form_data: "1" 
 
}; //Array 
 

 
$.ajax({ 
 
    url: "http://api.ecoachlabs.com/v1/requests/api.php", 
 
    type: "POST", 
 
    data: formData, 
 
    success: function(data, textStatus, jqXHR) { 
 
    console.log(data.ecoachlabs.categories); //data - response from server 
 
    $.each(data.ecoachlabs.categories, function(i, v) { 
 
     $('#institution_category').append($('<option value="' + v.id + '">' + v.category + '</option>')); 
 
    }); 
 
    }, 
 
    error: function(jqXHR, textStatus, errorThrown) { 
 

 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select id='institution_category'> 
 

 
</select>

+1

這怎麼可能跳過了我:)謝謝lonut – SoftServe

+0

它發生在我們所有人身上。 @SoftServe,不客氣。 – Ionut

0

檢查是否存在之前繼續

if (!data || !data.length) return;

<script type="text/javascript"> 
     $(document).ready(function() { 
     $("#institution_category").click(function(){ 

      var formData = {load_request_form_data:"1"}; //Array 

      $.ajax({ 
       url : "http://api.ecoachlabs.com/v1/requests/api.php", 
       type: "POST", 
       data : formData, 
       success: function(data, textStatus, jqXHR) 
       { 
        if (!data || !data.length) return; // data not always exist or not always contains ecoachlabs/categories 
        console.log(data.ecoachlabs.categories);//data - response from server 
        $.each(data.categories, function(i, v){ 
         $('#institution_category').append($('<option value="' + v.id + '">' + v.category + '</option>')); 
        }); 
       }, 
       error: function (jqXHR, textStatus, errorThrown) 
       { 

       } 
      }); 
     }); 
     }); 
    </script> 
相關問題