[英]AJAX returning 404 Not Found
我正在嘗試使用ajax調用從數據庫中獲取數據,但它返回此錯誤:
這是我的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個小時,但是沒有一個解決我的問題。
謝謝您的幫助。
只需檢查您是否以post方法或get方法發布數據。 嘗試使用以下函數來調用Ajax:
var request = $.ajax({
url: "yourURl",
type: "POST",
dataType: "json"
});
您的控制器不存在您期望的位置。 右鍵單擊錯誤消息中的URL,如果您使用的是Chrome,請嘗試“在新標簽頁中打開”。 這與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/$1 [L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.