【问题标题】:Error 404 getJson retrieving a local json file in Codeigniter错误 404 getJson 在 Codeigniter 中检索本地 json 文件
【发布时间】:2017-05-25 17:52:25
【问题描述】:

我正在尝试使用 CodeIgniter$.getJson 在脚本的同一文件夹中加载 Json 文件。

我已经尝试在我的站点根目录和应用程序文件夹中更改 .htaccess 内容,将加载路径更改为

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

没有任何效果。它总是显示相同的错误:

jquery.min.js:4 获取 http://www.my-site.com/index.php/admin_cotrol/shop_list.json 404(未找到)

这是我的脚本:

$(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);
        }); 
    });
});

【问题讨论】:

  • 您将 JSON 文件放在文件夹层次结构中的什么位置?
  • @squiroid 我将脚本和 json 存档放在同一个文件夹中。根文件夹
  • 那为什么无法通过您提供的 URL 访问它? my-site.com/index.php/admin_cotrol/shop_list.json
  • 我也进不去
  • 使用基本路径访问文件&lt;?php echo base_url()/folder/shop_list.json ?&gt;

标签: php jquery json ajax codeigniter


【解决方案1】:

建议将文件移至根目录之外的新目录。

例如: /static/json/shop_list.json

然后你就可以使用:

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

【讨论】:

  • 不起作用。现在向我显示此错误:http://www.my-site.com/test_things/files_list.json 404(未找到)
【解决方案2】:

首先将您的 json 文件放在具有资产文件夹的根目录中..即 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);
        }); 
    });
});

【讨论】:

  • 非常感谢!这解决了我的主要问题。但是现在又出现了一个,不,检索到的搜索结果。不过非常感谢。
  • console.log(data); 可能是 json 格式..所以你必须先JSON.parse(data) 才能转换成对象。
  • 现在它给我一个不同的错误代码,但基本上是一样的。我完全按照你的建议做,它有同样的错误。获取my-site.com/assets/shop_list.json 404(未找到)
  • 请再次帮助我
  • 如果你没有管理.htaccess,那么url必须是var url = "&lt;?php echo base_url('index.php/assets/shop_list.json');?&gt;";
猜你喜欢
  • 1970-01-01
  • 2012-01-21
  • 2012-12-25
  • 2019-07-14
  • 1970-01-01
  • 1970-01-01
  • 2019-05-30
  • 2012-06-20
相关资源
最近更新 更多