2016-01-24 293 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, 
       })); 
      }) 
     } 
    }); 
}); 
相關問題