AJAX 返回 404 未找到

AJAX returning 404 Not Found

我正在尝试使用 ajax 调用从我的数据库中获取数据,但是 returns 这个错误:

这是我的AJAX电话

$.ajax({ 
   url : '<?php echo base_url('Create_controller/getCategory'); ?>',
   dataType : 'json',
   success: function(data) {
       $(data).each(function(){
            $('#create_category').append($('<option>', {
                 value: this.id,
                 text: this.category,
             }));
        })
   },
   error: function(errorw) {
       alert("error");
    }
});

这是我的 Create_controller:

public function getCategory(){
    $categories = $this->create_model->getCategory();
    echo json_encode($categories);
}

这是我的 Create_model:

function getCategory(){
    $this->db->select('id, category');
    $this->db->from('category');
    $this->db->where('status', 1);
    $this->db->order_by('category', 'asc');
    $query = $this->db->get();
    return $query->result();
}

我知道我的控制器和模型可以正常工作,因为我在加载视图之前尝试使用 print_r($this->create_model->getCategory());

我现在已经搜索了 3 个小时,但其中 none 解决了我的问题。

感谢您的帮助。

只需检查您是否在 post 方法或 get 方法中 posting 数据。试试这个函数来调用 Ajax:

var request = $.ajax({
  url: "yourURl",
  type: "POST",
  dataType: "json"
});

您的控制器不在您期望的位置。右键单击错误消息中的 URL,如果您在 Chrome 中,请尝试 "open in new tab"。这与Ajax无关。这与您的 MVC 设置和路由有关。

这是临时的,但会替换

'<?php echo base_url('Create_controller/getCategory'); ?>'

"<?php echo base_url('Create_controller/getCategory'); ?>"

然后说它是否有效...

这个问题的原因是我忘记添加我的 .htaccess 文件。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]