2017-01-11 43 views
1

我使用的是CodeIgniter$.getJson試圖加載一個JSON文件,在我的腳本的同一文件夾中。錯誤404的getJSON獲取本地JSON文件笨

我已經嘗試改變我的網站的根目錄下,並在應用程序文件夾中的.htaccess內容,改變加載路線

.$getJson("<?php echo base_url...") 

並沒有什麼工作。它總是顯示了同樣的錯誤:

jquery.min.js:4 GET http://www.my-site.com/index.php/admin_cotrol/shop_list.json 404 (Not Found)

這裏是我的腳本:

$(window).load(function() { 
    $('#search').keyup(function() { 
     var searchField = $('#search').val(); 
     var regex = new RegExp(searchField, "i"); 
     var output = '<div class="row">'; 
     var count = 1; 
     $.getJSON("shop_list.json", function(data) { 
      $.each(data, function(key, val) { 
       if ((val.name.search(regex) != -1) || (val.location.search(regex) != -1)) { 
        output += '<div class="col-md-6 well">'; 
         output += '<div class="col-md-7">'; 
          output += '<h5>' + val.productName + '</h5>'; 
          output += '<p>' + val.productPrice + '</p>' 
          output += '<p>' + val.ProductDiscount + '</p>' 
         output += '</div>'; 
        output += '</div>'; 
        if(count%2 == 0) { 
         output += '</div><div class="row">' 
        } 
        count++; 
       } 
      }); 
      output += '</div>'; 
      $('#show_results').html(output); 
     }); 
    }); 
}); 
+0

您將JSON文件放在文件夾層次結構中的位置? – squiroid

+0

@squiroid我把劇本和在同一文件夾中的JSON檔案。根文件夾 –

+0

那麼爲什麼不通過你給的URL訪問? http://www.my-site.com/index.php/admin_cotrol/shop_list.json – squiroid

回答

1

建議把該文件到一個新的目錄了你的根。

例: /static/json/shop_list.json

然後,你將能夠使用訪問:

$.getJSON("/static/shop_list.json", function(data) { 
+0

不起作用。現在顯示我這個錯誤:HTTP:/www.my-site.com/test_things/files_list.json 404(未找到) –

0

首先把你的JSON文件根有資產folder..ie assets/shop_list.json 。然後使控制器在具有application/controller功能loadjson()保存爲Json.php

class Json extends CI_Controller 
{ 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper('url'); //loads url helper 
    } 

    public function loadjson() 
    { 
     $this->load->view('json'); 
    } 
} 

內部application/views創建視圖json.php具有腳本...

$(window).load(function() { 
    var url = "<?php echo base_url('assets/shop_list.json');?>"; 
    $('#search').keyup(function() { 
     var searchField = $('#search').val(); 
     var regex = new RegExp(searchField, "i"); 
     var output = '<div class="row">'; 
     var count = 1; 
     $.getJSON(url, function(data) { 
      $.each(data, function(key, val) { 
       if ((val.name.search(regex) != -1) || (val.location.search(regex) != -1)) { 
        output += '<div class="col-md-6 well">'; 
         output += '<div class="col-md-7">'; 
          output += '<h5>' + val.productName + '</h5>'; 
          output += '<p>' + val.productPrice + '</p>' 
          output += '<p>' + val.ProductDiscount + '</p>' 
         output += '</div>'; 
        output += '</div>'; 
        if(count%2 == 0) { 
         output += '</div><div class="row">' 
        } 
        count++; 
       } 
      }); 
      output += '</div>'; 
      $('#show_results').html(output); 
     }); 
    }); 
}); 
+0

非常感謝!這解決了我的主要問題。但是,現在另一個請求,不,檢索結果。但是非常感謝你。 –

+0

'的console.log(數據);'可以是JSON format..so你必須'JSON.parse(數據)'第一轉換成物體。 –

+0

現在它拋出了一個不同的錯誤代碼,但是非常相似。我完全按照你的建議去做,而且它也有同樣的錯誤。 GET http://www.my-site.com/assets/shop_list.json 404(未找到) –