json对象没有在php中返回

时间:2013-05-07 06:35:32

标签: php json

我有一个函数可以在一个变量中获取两个数组并像这样创建json

function get_categories_n_subs_by_id($id){ ///this is category id;
    header('Content-type: application/json');
    $data['categories'] = $this->categories_model->get_categories_by_id($id);
    $data['sub_categories'] = $this->sub_categories_model->get_sub_categories_by_cat_id($id);
    echo json_encode($data);
}

此代码运行良好,但是当我重新启动电脑时,json没有响应。

早些时候的反应很好,就像这样

{
  "categories":{
    "fld_id":"106",
    "fld_value":"some value",
    "fld_price":"0.00",
    "fld_attribute_id":"16",
    "fld_parent_id":"0" 
  },
  "sub_categories":[{
    "fld_id":"107",
    "fld_value":"some sub values",
    "fld_price":"0.00",
    "fld_attribute_id":"16",
    "fld_parent_id":"106"
   }]
}

但现在没有回应。 json对此函数的调用是:

var url = base_url+"admin/categories_controller/get_categories_n_subs_by_id/"+id;
    $.getJSON(url, {ajax:1}, function(data){
        var id = data['categories']['fld_id'];
        var name = data['categories']['fld_name'];
        var description = data['categories']['fld_description'];
        //var status = data['fld_status'];

        $("#new_category #name").attr('value',name);
        $("#new_category #description").html(description);
        $("#new_category #category_id").attr('value',id);
        var html="";
        if(data['sub_categories'].length==0){
            html = html + 'Sub Categories: <input type="text" id="sub_name[]" name="sub_name[]" maxlength="40"  value=""/>'+
                        ' Description: <textarea id="sub_description[]" name="sub_description[]"></textarea>'+
                        '<input type="hidden" id="sub_cat_id[]" name="sub_cat_id[]" value="">'+
                        '&nbsp;&nbsp;&nbsp;&nbsp;'+
                        '<a href="javascript:void(0);" class="add_attribute_values" onclick="add_sub_categories(this.name);" name="sub_cat_0">[+]</a>&nbsp;&nbsp;<br/>';
        }
        $.each(data['sub_categories'], function(index, array) {
            var id = array['fld_id'];
            var name = array['fld_name'];
            var description = array['fld_description'];
            html = html + 'Sub Categories: <input type="text" id="sub_name[]" name="sub_name[]" maxlength="40"  value="'+name+'"/>'+
                        ' Description: <textarea id="sub_description[]" name="sub_description[]">'+description+'</textarea>'+
                        '<input type="hidden" id="sub_cat_id[]" name="sub_cat_id[]" value="'+id+'">'+
                        '&nbsp;&nbsp;&nbsp;&nbsp;'+
                        '<a href="javascript:void(0);" class="add_attribute_values" onclick="add_sub_categories(this.name);" name="sub_cat_0">[+]</a>&nbsp;&nbsp;<br/>';
        });
        //alert(html);
        $("#sub_cat_0").html(html)
    });

在我重新启动电脑之前,一切都是正确的,但在此之后,当我通过萤火虫检查时,json没有回复。响应只是空的......请帮我解决这个错误...

1 个答案:

答案 0 :(得分:0)

问题已解决..在重新启动我的电脑之前,我使用的是ip地址,如:

http://192.168.1.2

重新启动后我正在使用

http://localhost

并且在codeigniter配置文件中我配置为http://192.168.1.2影响了json ...所以你们这样做时要记住它......