【问题标题】:404 on http request404 上的 http 请求
【发布时间】:2019-05-03 08:34:13
【问题描述】:

当我尝试向 codeigniter 路由发出 ajax 请求时,我收到 404 错误

项目的根文件夹是http://localhost/control_cuotas/

这是控制器(索引有效):

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Control_cuotas_controller extends CI_Controller {

    public function index()
    {
        $data = array();
        $tables = $this->getTables();
        $data['tables'] = $tables;
        $this->load->view('main/main_view', $data);      
    }

    public function getData($data){
        var_dump($data);
    }

    private function getTables(){

        $sql = "SELECT TABLE_NAME " 
            ."FROM INFORMATION_SCHEMA.TABLES "
            ."WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME LIKE 'datos_%'";
        $query = $this->db->query($sql);
        $tables = array();

        foreach ($query->result() as $row){
            $name = substr($row->TABLE_NAME,0,-2);
            if(!in_array($name,$tables)){
                array_push($tables, $name);
            }
        }

        return $tables;
    }

这是javascript:

$(document).ready(function(){

    $('#tables').change(function(){
        var selected = $(this).val();
        $.ajax({
            url:'getData/'+selected
        });
    });
});
}

这是路线

$route['getData/(:any)']['GET'] = 'control_cuotas_controller/getData/$1';

请求在以下网址完成

http://localhost/control_cuotas/getData/selected_value

【问题讨论】:

    标签: php ajax codeigniter codeigniter-3


    【解决方案1】:

    请检查 .htaccess 文件

    并尝试此代码;

    RewriteEngine on
    RewriteCond $1 !^(index\.php|public|\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1
    

    $(document).ready(function(){
    
        $('#tables').change(function(){
            var selected = $(this).val();
            $.ajax({
                url:'http://localhost/control_cuotas/getData/'+selected
            });
        });
    });
    }
    

    【讨论】:

      【解决方案2】:

      您尚未添加 .htaccess 代码以从 URL 中删除 index.php。要么这样做,要么在你的 JavaScript 中将 URL 更改为 http(:)//localhost/control_cuotas/index.php/getData

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-07
        • 2020-07-21
        相关资源
        最近更新 更多