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