【问题标题】:json_encode() value does not propagate right formatjson_encode() 值不传播正确的格式
【发布时间】:2017-03-09 07:00:18
【问题描述】:

我正在使用 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);
}
});

【问题讨论】:

  • 先检查你的base_url。
  • 首先通过执行console.log(url)检查你的'url'。无论您是否获得正确的网址。似乎它没有正确获取服务器 url。
  • url: "<?php echo base_url()?>/index.php?module=sales&view=getproductinfo",

标签: json ajax codeigniter


【解决方案1】:

也许你可以使用 ../index.php?module=sales&view=getproductinfo 和错误 console.log(respon) 来解决你的问题

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      相关资源
      最近更新 更多