2013-10-08 68 views
0

我正在嘗試使用下面的jQuery進行ajax調用。JSON或Jquery錯誤:未捕獲TypeError:無法讀取null的屬性'錯誤'

但我可以在Chrome中看到,我得到未捕獲的typeerror無法讀取null屬性的'錯誤'。所以這阻止了預加載程序離開。任何想法爲什麼這樣做?

function appendTeam(){ 

$.ajax({ 

     url : _path + "/core/ajax.php", 
     type : 'POST', 
     data : { f: 'getTeam'}, 
     dataType : 'json', 
     success : function(data) { 

     if(data.error){ 

      errorMessage('Error: ' + data.error, data.error, data.error); 
      return false; 

     }else{ 
      var count = 0; 
      $.each(data.team, function(i, c){ 
       // check 
       if(!$('#'+c)) return true; 
       var element = $('#'+c); 
       $('input[name="s'+i+'"]').val(element.attr('id')); 
       $('.slot.'+(i+1)+'.ui-droppable').append(element); 
       element.data('prevParent', $('.slot.'+(i+1)+'.ui-droppable')); 
       count ++; 

      }); 

      appendStatus(count); 
      setTimeout(function(){ 
       $('#preloader').fadeOut('fast',function(){ 
        $('#preloader').remove(); 
        popUp('match'); 
       }); 
      }, 2000); 


     } 

    } 
}); 
} 
+0

您可以添加Ajax.php被渲染JSON的例子反應? –

+2

此外成功回調不需要/使用返回語句 –

回答

3

。在你的if運營商的錯誤:

if(data.error)...

您應該檢查它像if(data && data.error)...因此,如果服務器返回你null而不是JSON對象,你會不會嘗試訪問對象財產error

而且也許你需要單獨處理這種情況:嘗試存取權限他error財產

+0

這工作,謝謝大家的幫助。 –

0

測試是否datanull空值;

所以不是這個

if(data.error) {

,你可以用不同的方式檢查它:

if(data && data.hasOwnProperty('error')) {

這是比較失敗的,安全的。

0

之前也許data對象不包含屬性error或者是

if(!data) { 
    // handle empty response 
} else if(data.error) { 
    // handle error 
} else { 
    // process results 
} 
0

我想回應是空嘗試,看看在使用Firebug enter image description here

相關問題