[UPDATE]:在config.php中:$ config ['base_url'] ='http://localhost/posts/';
我正在使用控制器文件ajax_controller.php来处理视图页面中的ajax帖子
$(document).on('click','a.delete',function (e) {
e.preventDefault();
var id = $(this).attr('id');
noty({
text : 'post will be deleted',
type : 'alert',
dismissQueue: true,
layout : 'center',
theme : 'defaultTheme',
modal : true,
buttons : [
{addClass: 'btn btn-primary', text: 'Ok', onClick: function ($noty) {
$.ajax({
type: "POST",
url: "<?=base_url()?>" + "ajax_controller/del_post",
data: {id: id},
dataType: "text",
cache:false,
success:
function(data){
//alert(data);
$noty.close();
noty({dismissQueue: true, force: true, layout: 'center', theme: 'defaultTheme', text: 'You clicked "OK" button', type: 'success',timeout:'2000'});
}
});
}
},
{addClass: 'btn btn-danger', text: 'Cancel', onClick: function ($noty) {
$noty.close();
noty({dismissQueue: true, force: true, layout: 'center', theme: 'defaultTheme', text: 'You clicked "Cancel" button', type: 'error',timeout:'2000'});
}
}
]
});
});
控制器包含此代码
class ajax_controller extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model('posts_model');
}
function del_post($postID){
$this->posts_model->del_post($postID);
echo 'success';
}
}
posts_model包含这个函数(和其他函数工作正常)
function del_post($postID){
$this->db->delete()->from('posts')->where('Post_ID',$postID);
}
但是,当我单击删除按钮时,我收到此错误
http://[::1]/posts/ajax_controller/del_post 500 (Internal Server Error)
我把网址改成了
url: "<?=base_url()?>" + "ajax_controller/del_post/"+id,
和注释数据:{id:id},但我得到了同样的错误 . 所以我的问题是如何通过编辑这个ajax在codegniter中正确创建一个ajax .
1 回答
根据你的帖子,请求的URL是错误的,好像它在localhost上应该是这样的:
http://127.0.0.1/posts/ajax_controller/del_post
要么
http://localhost/posts/ajax_controller/del_post
尝试在
config.php
中设置base_url,它应该可以解决您的问题 .让我知道你的疑问 .
----EDIT------
你写了错误的删除查询,它应该是这样的
有关详细信息,请参阅此链接https://www.codeigniter.com/userguide2/database/active_record.html#delete