【发布时间】: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