AJAX返回404 Not Found

时间:2016-03-01 03:10:12

标签: php jquery ajax codeigniter-3

我正在尝试使用ajax调用从我的数据库中获取数据,但它返回此错误:

enter image description here

这是我的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个小时,但没有一个能解决我的问题。

感谢您的帮助。

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您的控制器并不存在于您期望的位置。右键单击错误消息中的URL并尝试&#34;在新选项卡中打开&#34;如果你在Chrome中。这与Ajax无关。它与您的MVC设置和路由有关。

答案 2 :(得分:0)

这是暂时的,但是替换

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

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

并说它是否有效......

答案 3 :(得分:0)

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

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