【问题标题】:codeigniter ajax post 'internal server error' on localhostcodeigniter ajax 在本地主机上发布“内部服务器错误”
【发布时间】: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


【解决方案1】:

根据您的帖子,请求的 URL 是错误的,就好像它在 localhost 上一样,应该是这样的: http://127.0.0.1/posts/ajax_controller/del_post

http://localhost/posts/ajax_controller/del_post

尝试将您的 base_url 设置为 config.php,它应该可以解决您的问题。

让我知道您的疑问。

----编辑------

你写错了删除查询,应该是这样的

$this->db->delete('mytable', array('id' => $id)); 

请参阅此链接了解更多信息 https://www.codeigniter.com/userguide2/database/active_record.html#delete

【讨论】:

  • $config['base_url'] = 'localhost/posts';但同样的错误“localhost/posts/ajax_controller/del_post/2500(内部服务器错误)”
  • 在您的配置中尝试使用http://localhost/post,然后尝试使用URL http://localhost/posts/index.php/ajax_controller/del_post/2
  • 现在我在打开链接时收到此错误 您必须设置要与查询一起使用的数据库表。文件名:D:/xampp/htdocs/posts/system/database/DB_query_builder.php 行号:2140 我的查询:$this->db->delete()->from('posts')->where('Post_ID' ,$postID);
猜你喜欢
  • 2022-01-10
  • 1970-01-01
  • 2015-10-22
  • 2012-05-12
  • 1970-01-01
  • 2023-03-05
  • 2018-02-22
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多