我試圖在文檔上使用AJAX填充下拉菜單。禁止在此服務器上訪問
但是我無法訪問我的Supplies_controller,因爲我被禁止了。
我的populate_dropdown.js文件位於與我的Supplies_controller文件不同的文件夾中。
這是我已經得到
錯誤這是我在我的populate_dropdown.js代碼
$(document).ready(function() {
$.ajax({
url: "<?php echo base_url('Supplies_controller/getCategory'); ?>",
dataType: 'json',
success: function(data) {
alert(data);
$(data).each(function(){
$("#category").append($('<option>', {
value: this.id,
text: this.category,
}));
})
}
});
});
這是我Supplies_controller
<?php
class Supplies_controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('supplies_model');
}
public function getCategory(){
$categories = $this->supplies_model->getCategory();
echo json_encode($categories);
}
public function getSubcategory(){
$category_id = $this->input->post('category');
$subcategories = $this->supplies_model->getSubCategory($category_id);
echo json_encode($subcategories);
}
public function getSupply(){
$subcategory_id = $this->input->post('category');
$supplies = $this->supplies_model->getSubCategory($subcategory_id);
echo json_encode($supplies);
}
代碼
這是我的文件的層次結構
我Supplies_controller是控制器文件夾內,我的populate_dropdown.js文件是在js文件夾內。
請幫我找到我的錯誤。謝謝。
PHP無法解析/讀/在js文件執行。你必須把你的js代碼放在視圖文件中。 – Tpojka