【发布时间】:2017-01-26 02:53:02
【问题描述】:
[更新]:在 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中正确创建一个post ajax。
【问题讨论】:
-
您没有正确获取 base_url(),请尝试提醒或记录
base_url() -
base_url: http://[::1]/posts/
标签: php jquery ajax codeigniter