基于ajax后台返回的数据为空前台显示出现undefined的解决方法

之前自己做的一个图书管理系统,显示图书借阅排行榜,因为翻译在数据库中有为空的字段,故前台显示会显示undefined。

以下贴上部门代码:

$(document).ready(function () { 
 $("#rankTable tbody").html("");

 var id = 1;

  $.ajax({ 
   url:"bookInfo/bookInfo_rank.action", 
   type:"POST", 
   data:{}, 
   dataType:"json", 
   success:function (data) { 
   
     $(data).each(function(i,item)
     {
     var index = id++;
     var bookId = item.bookId;
     var bookName = item.bookName;
     var author = item.author;
     //判断是否为空出现undefined;
     var translator = (item.translator==undefined ?"":item.translator);
     var ISBN = item.ISBN;
     var press = item.press;
     var page = item.page;
     var bookCase = item.bookCase;
     var totalNum = item.totalNum;
     var stock = item.stock;
     var borrowNum = item.borrowNum;
     var typeName = item.typeName;
     var str = '<tr> ' + '<td>' + index + 
     '</td>' + '<td>' + bookId + 
     '</td>' + '<td>' +bookName + 
     '</td>' + '<td>' + author+
     '</td>' +'<td>'+translator+
     '</td>' +'<td>'+ISBN+
     '</td>' +'<td>'+press+
     '</td>' +'<td>'+page+
     '</td>' +'<td>'+bookCase+
     '</td>' +'<td>'+totalNum+
     '</td>' +'<td>'+stock+
     '</td>' +'<td>'+borrowNum+
     '</td>' +'<td>'+typeName+
     '</td></tr>';
     
     $("#rankTable tbody").append(str);
     str = "";
     
     })
     
   },
   error:function(XMLHttpRequest, textStatus, errorThrown){
   alert("系统异常,请稍后重试!");
   alert(XMLHttpRequest.status);
      alert(XMLHttpRequest.readyState);
      alert(textStatus);
   } 
  })
});

相关推荐