【问题标题】:network error 500 while trying codeigniter controller action using ajax尝试使用 ajax 的 codeigniter 控制器操作时出现网络错误 500
【发布时间】:2016-05-25 14:24:10
【问题描述】:

我有一个管理控制器,里面有一个 processReq 函数。这里我有一个按钮,可以在单击时访问控制器操作。但是每当我单击该按钮时,它都会给我一个错误提示

"NetworkError: 500 内部服务器错误 - http://localhost/mycodeigniter/ci/index.php/Admin/processReq"

我使用 htaccess 文件隐藏了我的 index.php

管理员控制器:

   class Admin extends CI_Controller{

         public function processReq(){
             $status=$this->item->post('status');
             echo $status;
         }

   }

和 ajax 请求是:

<button type="button" data-id='approved' class="btn btn-success approved buttons"  style="margin-right:15px;font-weight:bold;">Approve</button>

ajax:

$(".buttons").click(function(event){
     var status=$(this).data('id');
     $.ajax({
        url: '<?php echo base_url('Admin/processReq'); ?>',
        type: 'POST',
        data: {
            'status': status
        },
        dataType: 'json',
        success: function(data) {
            alert('success');
        }
    });
 });

【问题讨论】:

  • 您的错误日志中是否记录了错误?对于可能位于 /var/log/apache2/error.log 中的 Apache
  • 你有语法错误。应该是这样的url: '&lt;?php echo base_url("Admin/processReq"); ?&gt;',注意单引号和双引号。

标签: php jquery ajax codeigniter


【解决方案1】:

更改网址

url: '<?php echo base_url('Admin/processReq'); ?>',

到这里

url: '<?php echo base_url(); ?>index.php/Admin/processReq',

index.php 添加到 URL


改成

$status=$this->item->post('status');

这个

$status=$this->input->post('status');

编辑 01

<script>
    $(function(){
        $(".buttons").click(function(event)
        {
            var status=$(this).data('id');
            $.ajax(
                {
                    type:"post",
                    url: "<?php echo base_url(); ?>index.php/admin/processReq",
                    data:{ status:status},
                    success:function(data)
                    {
                        alert('success');
                    }
                });
        });
    });
</script>

【讨论】:

  • 你设置了 $config['base_url'] ?
  • 它有 http..当我在评论中发帖时 http 被删除...在我将项目更改为输入后..错误消失了...但要求不访问控制器....我试图在控制器的功能内回显,,,它没有触发..这意味着ajax请求没有调用控制器操作..它
  • 尝试Adminadmin 并尝试
  • 我需要创建一个名为 processReq.php 的页面吗?
  • 注释$(".buttons").click(function(event){中的所有代码并添加alert('here');并检查输出。 我认为你的 AJAX 没有触发
【解决方案2】:

首先你有语法错误。

url: '&lt;?php echo base_url("Admin/processReq"); ?&gt;', 应该是这样的

url: '<?php echo base_url("Admin/processReq"); ?>',

其次,如果您使用 .htaccess 从 url 隐藏 index.php。这意味着当您在 ajax 请求中看到 index.php 时,它不会生成正确的 url。

转到您的 application/config.php 并像这样设置 base_url

$config['base_url'] = ' http://localhost/mycodeigniter/ci/';

并设置

$config['index_page'] = '';
//remove index.php from this line
//whatever you set here it will be included to your base_url when you use site_url function.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-10
    • 2020-03-16
    • 2015-11-04
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多