2016-07-07 108 views
0

我想發佈一些數據到CodeIgniter控制器,但它只顯示空數組。我也在使用DataTables。我不確定我是否以正確的方式發送數據。這是我的代碼:Ajax發佈到CodeIgniter控制器不工作

JS

$("#prw").on('click', function(e){ 
    e.preventDefault(); 

    var url = window.location.origin + "/nsl/preview"; 

    $.ajax({ 
     url: url, 
     data: $("input[type='checkbox']:checked").serialize() 
    }).done(function(data){ 
     console.log("Response", data); 
    }); 
}); 

控制器

function preview() { 
    $post = $this->input->post(); 
    return $post; 
} 

HTML

<table id="offers" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
     <thead> 
     <tr> 
      <th>...</th> 
      <th>...</th> 
      <th>...</th> 
      <th>...</th> 
      <th>...</th> 
     </tr> 
     </thead> 
     <tfoot> 
     <tr> 
      <th>...</th> 
      <th>...</th> 
      <th>...</th> 
      <th>...</th> 
      <th>...</th> 
     </tr> 
     </tfoot> 
     <tbody> 
     <?php foreach($data as $d): ?> 
     <tr> 
      <td><input type="checkbox" id="check" name="myCheckboxes[]" class="toggle" value="<?=$d->oid?>"></td> 
      <td><input type="text" id="rb" name="myText[]" class="toggle"></td> 
      <td><?=$d->fullname?></td> 
      <td><?=$d->published?></td> 
      <td><?=$d->expires?></td> 
     </tr> 
     <?php endforeach; ?> 
     </tbody> 
    </table> 
    <input type="submit" id="prw" class="btn btn-info" value="Preview"> 

請我感謝所有幫助。

+0

通常情況下,ajax調用應該可以正常使用正確的MVC路徑。一定要指向正確的控制器/方法。然後嘗試使用.beforeSend()首先調試ajax,並查看您的「數據」是否正常。接下來 - 簡化你的控制器方法來返回你的$ _POST [] - 這會給你一個結束的確定性,即一切按預期工作 – wick3d

回答

1

有些事情要檢查: -

  1. 裏面的Ajax調用,何況type : 'post'

  2. 在點擊功能,嘗試

    警報($(「輸入[類型= '複選框' ]:checked「)。serialize());

    並檢查您是否按預期得到值?

  3. 打開Firebug,點擊控制檯,執行點擊按鈕,看看在控制檯裏面的URL的POST選項卡下有什麼值。

  4. 在控制器內部

    $張貼= $這個 - >輸入 - >交的( 'myCheckboxes'); print_r($ post);