【发布时间】:2018-02-05 01:05:29
【问题描述】:
我正在使用最新版本的数据表
我通过这样的 ajax 请求删除了一行:
$.ajax
(
{
method : 'POST',
//la route (controleur) et le paramètre (id à supprimer)
url: url + "/professionnels/deleteProfessionnels/"+id_to_delete,
dataType: 'json',
success:function()
{
//console.log(retour);
$('#example').DataTable().ajax.reload();
PHP模型代码:
public function deleteProfessionnel($pro_id)
{
$sql = "DELETE FROM professionnels WHERE id = :pro_id";
$query = $this->db->prepare($sql);
$parameters = array(':pro_id' => $pro_id);
// useful for debugging: you can see the SQL behind above construction by using:
//echo '[ PDO DEBUG ]: ' . Helper::debugPDO($sql, $parameters); exit();
$query->execute($parameters);
}
PHP 控制器代码:
我到达了ajax的成功函数,但是我得到了这个消息:
DataTables 警告:表 id=example - JSON 响应无效。有关此错误的更多信息,请参阅http://datatables.net/tn/1
public function deleteProfessionnels($professionnel_id)
{
// if we have an id of a song that should be deleted
if (isset($professionnel_id))
{
// Instance new Model (Song)
$professionnel = new Professionnel();
// do deleteSong() in model/model.php
$professionnel->deleteProfessionnel($professionnel_id);
//echo "id=> ".$professionnel_id;
}
// where to go after song has been deleted
$professionnels = $professionnel->getAllProfessionnels();
//print_r(json_encode($professionnels));
echo json_encode($professionnels, JSON_FORCE_OBJECT);
当我使用enter link description here 测试 json 结果时,出现错误。
1- 为什么 php 返回一个无效的 json 对象? 2-我必须做所有这些来重新加载表格吗?
这是js中的响应(json对象):
{"0":{"id":"123","nom":"G\u00e9om\u00e8tre","prenom":"Barack","adresse":null,"numero":null,"boite":null
,"cp":null,"ville":null,"mail":null,"telephone":null,"inami":null,"tva":null,"disponibilite":null,"commentaire"
:null},"1":{"id":"128","nom":"G\u00e9om\u00e8tre-expert","prenom":"Barack","adresse":null,"numero":"6"
,"boite":"3","cp":"1300","ville":"Bruxelles","mail":"contact@dubinfo.be","telephone":"0471301253","inami"
:"5533434343","tva":"BE 0832.581.586","disponibilite":null,"commentaire":null}}
【问题讨论】:
-
请先在浏览器中检查您在 ajax 中使用的 url 是否得到有效响应?
-
我试了几次显示返回,暂时没有成功。
标签: php jquery ajax datatables