json_encode()值不会传播正确的格式

时间:2017-03-09 07:00:18

标签: json ajax codeigniter

我正在使用json_encode函数来获取ajax响应中的值。

$product_id = $this->input->get('product_id');
        if ($data = $this->sales_model->getProductById($product_id)) {
            //$product_id     = $data->id;
            $product_name   = $data->name;
            $product_unit   = $data->product_unit;
            $sales_price    = $data->sales_price;
            $product_details = array('product_id' => $product_id, 'product_name' => $product_name, 'product_unit' => $product_unit, 'sales_price' => $sales_price);
            //add the header here
            header('Content-Type: application/json');
            echo json_encode($product_details);
        }

然后在本地服务器中获取值

Object {product_id: "77", product_name: "Testdescription", product_unit: "", sales_price: "120.00"}

但是当我将其上传到服务器时,我们没有得到任何回复。

ajax调用函数:

 $.ajax({
                type: "get",
                url: "index.php?module=sales&view=getproductinfo",
                dataType: "json",
                contentType:"application/json",
                data: {product_id: data_id},
                error: function() {
                    $('#info').html('sss');
                },
                success: function (response) {
                    console.log(response);
}
});

2 个答案:

答案 0 :(得分:0)

也许你可以使用../index.php?module=sales&view=getproductinfo,并在错误console.log(响应)上来解决你的问题

答案 1 :(得分:0)

您已经检查过您在prodict_id中发送的data_id是否正确传递。如果没有传递,那么响应将为空白而没有任何错误。