我正在尝试使用 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 位于 Controller 文件夹内,我的 populate_dropdown.js 文件位于 js 文件夹内。
请帮我找出我的错误。谢谢。
最佳答案
尝试使用 var 变量作为 base_url
查看文件脚本
<script type="text/javascript">
$(document).ready(function() {
var base_url = "<?php echo base_url();?>";
$.ajax({
url: base_url + "supplies_controller/getCategory",
dataType: 'json',
success: function(data) {
alert(data);
$(data).each(function(){
$("#category").append($('<option>', {
value: this.id,
text: this.category,
}));
})
}
});
});
</script>
或者 java 脚本文件
$(document).ready(function() {
$.ajax({
url: "supplies_controller/getCategory",
dataType: 'json',
success: function(data) {
alert(data);
$(data).each(function(){
$("#category").append($('<option>', {
value: this.id,
text: this.category,
}));
})
}
});
});
在你的 config.php 上设置你的 base_url。
$config['base_url'] = 'http://localhost/yourproject/';
Note: Make sure your assets folder is out side of application folder.
关于javascript - 禁止访问该服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34974384/