2016-01-24 71 views
1

我试图在文档上使用AJAX填充下拉菜单。禁止在此服务器上访问

但是我无法访问我的Supplies_controller,因为我被禁止了。

我的populate_dropdown.js文件位于与我的Supplies_controller文件不同的文件夹中。

这是我已经得到

enter image description here

错误这是我在我的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);  
} 
代码

这是我的文件的层次结构

enter image description here

我Supplies_controller是控制器文件夹内,我的populate_dropdown.js文件是在js文件夹内。

请帮我找到我的错误。谢谢。

+0

PHP无法解析/读/在js文件执行。你必须把你的js代码放在视图文件中。 – Tpojka

回答

1

使用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/'; 

注:请确保您的资产文件夹是从应用程序文件夹的一面。

+0

URL:“Supplies_controller/getCategory”,工作。谢谢。 –

+0

您的欢迎第一个可用于视图。 – user4419336

0

尝试改变这种

url: "<?php echo base_url('Supplies_controller/getCategory'); ?>", 

这个

url: "<?php echo site_url('Supplies_controller/getCategory'); ?>", 

啊我看你现在不能运行里面的JavaScript代码的PHP。 如果你想在JavaScript中,你需要这样的

url: location.protocol + "//" + location.host + "/ppmp/index.php/Supplies_controller/getCategory" , 
+0

所以我试过这个网址:location.protocol +“//”+ location.host +“/index.php/Supplies_controller/getCategory”,。我没有收到任何错误,但同时我没有收到任何结果。 –

+0

@CronasDeSe确定我编辑再试 – synan54

0

你需要复制的URL,然后包括JS

<script> 
     var URL = "<?php echo base_url('Supplies_controller/getCategory'); ?>"; 
    </script> 
    <script src="populate_dropdown.js"></script> 

然后在js文件,你可以使用可变URL

$(document).ready(function() { 
    $.ajax({ 
     url: URL, 
     dataType: 'json', 
     success: function(data) { 
      alert(data); 
      $(data).each(function(){ 
       $("#category").append($('<option>', { 
        value: this.id, 
        text: this.category, 
       })); 
      }) 
     } 
    }); 
}); 
相关问题