localhost工作正常,托管错误“未捕获的TypeError:无法读取null的属性'记录'

时间:2017-10-26 11:38:47

标签: javascript php json ajax codeigniter

我遇到了一个错误:

**Uncaught TypeError: Cannot read property 'record' of null.**

我想通过表中的ajax显示数据,但是存在问题。当我从localhost打开页面时,它运行正常,但是当我从托管站点打开它时, 是一个问题。

 if(data.record != null ) {
    $.each(data.record, function(index, element){
      var post_status = '';
      if(element.post_status == 'pending'){
        post_status = '(pending)';
      }

      $('table#tbl-artikel').find('tbody').append(
        '<tr>'+
        '  <td width="2%"><input type="checkbox" name="post_id[]" value="'+element.post_ID+'"></td>'+
        '  <td width="50%"><a class="link-edit" href="artikel#edit?id='+element.post_ID+'">'+element.post_title+'</a> <strong>'+post_status+'</strong></td>'+
        '  <td width="10%"><i class="icon-comment-alt"></i> <span class="value">'+element.comment_count+'</span></td>'+
        '  <td width="10%"><i class="icon-eye-open"></i> <span class="value">'+element.post_counter+'</span></td>'+
        '  <td width="12%"><i class="icon-time"></i> <span class="value">'+moment(element.post_date).fromNow()+'</span></td>'+
        '  <td width="16%" class="td-actions">'+
        '    <a href="artikel#edit?id='+element.post_ID+'" class="link-edit btn btn-small btn-info"><i class="btn-icon-only icon-pencil"></i> Edit</a>'+
        '    <a href="artikel#hapus?id='+element.post_ID+'" class="btn btn-invert btn-small btn-info"><i class="btn-icon-only icon-remove" id="hapus_1"></i> Hapus</a>'+
        '  </td>'+
        '</tr>'                
      )
    });
  }else{
    console.log(data);
  }

1 个答案:

答案 0 :(得分:1)

将您的代码更改为:

 if(data != null && data.record != null ) {
    $.each(data.record, function(index, element){
      var post_status = '';
      if(element.post_status == 'pending'){
        post_status = '(pending)';
      }

      $('table#tbl-artikel').find('tbody').append(
        '<tr>'+
        '  <td width="2%"><input type="checkbox" name="post_id[]" value="'+element.post_ID+'"></td>'+
        '  <td width="50%"><a class="link-edit" href="artikel#edit?id='+element.post_ID+'">'+element.post_title+'</a> <strong>'+post_status+'</strong></td>'+
        '  <td width="10%"><i class="icon-comment-alt"></i> <span class="value">'+element.comment_count+'</span></td>'+
        '  <td width="10%"><i class="icon-eye-open"></i> <span class="value">'+element.post_counter+'</span></td>'+
        '  <td width="12%"><i class="icon-time"></i> <span class="value">'+moment(element.post_date).fromNow()+'</span></td>'+
        '  <td width="16%" class="td-actions">'+
        '    <a href="artikel#edit?id='+element.post_ID+'" class="link-edit btn btn-small btn-info"><i class="btn-icon-only icon-pencil"></i> Edit</a>'+
        '    <a href="artikel#hapus?id='+element.post_ID+'" class="btn btn-invert btn-small btn-info"><i class="btn-icon-only icon-remove" id="hapus_1"></i> Hapus</a>'+
        '  </td>'+
        '</tr>'                
      )
    });
  }else{
    console.log(data);
  }