我试图在文档上使用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