简体   繁体   English

AJAX返回404找不到

[英]AJAX returning 404 Not Found

I am trying to get data from my database using ajax call but it returns this error: 我正在尝试使用ajax调用从数据库中获取数据,但它返回此错误:

在此处输入图片说明

This is my AJAX call 这是我的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");
    }
});

This is my Create_controller: 这是我的Create_controller:

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

This is my Create_model: 这是我的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();
}

I know that my controller and model works because I have tried using print_r($this->create_model->getCategory()); 我知道我的控制器和模型可以正常工作,因为我尝试使用print_r($this->create_model->getCategory()); before loading the view. 在加载视图之前。

I have been searching for 3 hours now but none of them solves my problem. 我已经搜索了3个小时,但是没有一个解决我的问题。

Thanks for the help. 谢谢您的帮助。

Just check that you are posting data in a post method or get method. 只需检查您是否以post方法或get方法发布数据。 Try this function to call Ajax: 尝试使用以下函数来调用Ajax:

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

Your controller doesn't exist where you expect it. 您的控制器不存在您期望的位置。 Right-click on to the URL in the error message and try "open in new tab" if you're in Chrome. 右键单击错误消息中的URL,如果您使用的是Chrome,请尝试“在新标签页中打开”。 This has nothing to do with Ajax. 这与Ajax无关。 It's related to your MVC setup and routing. 它与您的MVC设置和路由有关。

This is temporary but replace 这是暂时的,但要替换

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

with

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

and say if it works... 并说是否可行...

The cause of this problem is that I forgot to add my .htaccess file. 此问题的原因是我忘记添加.htaccess文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM