选择数量下拉按钮后,价格字段将相应更改。但是,我不断获得成功的警报,但是数据显示为空结果。
我尝试使用成功而不是完成,但是结果是相同的。我还删除了数据类型:json,仍然没有更改。
$('#qty').change(function(){
var val = $(this).val();
$.ajax({
method: "GET",
url: "someURLHERE",
data: { val: val},
dataType: "html"
})
.done(function(data) {
alert("Success" + data);
$('#price').html(data);
$('#price').trigger('change');
}).fail(function() {
alert("Fail");
})
});
这是我的模特:
public function getPrice($product_id, $quantity){
$sql = "SELECT price FROM `price_table` where quantity = '".$quantity."' and product_id = '".$product_id."' ";
$query = $this->db->query($sql)->row_array();
return $query;
}
这是我的控制器:
public function getPrice(){
$p_id = $_GET['p_id'];
$qty = $_GET['qty_ag'];
$price = $this->users_model->getPrice($prpduct_id, $quantity);
json_ok($price);
}
现在,警告框将返回:成功{“状态”:“确定”,“数据”:{“ pv_value”:“ 120.00”}}
我要获取的值是120.00
答案 0 :(得分:0)
您说您删除了dataType: "JSON"
,但是您要在服务器端(PHP)返回什么类型的数据,以及如何返回它?
例如
如果您期望使用JSON类型的数据,那么仍然可以在JavaScript中使用它:
$('#qty').change(function(){
var val = $(this).val();
$.ajax({
method: "GET",
url: "someURLHERE",
data: {
val: val
},
dataType: "JSON" // notice I've returned it to JSON
})
.done(function(data){
console.log("Success" + data); // notice I'm using "console.log"
$('#price').html(data);
$('#price').trigger('change');
})
.fail(function(){
alert("Fail");
})
});
但是,在服务器端,PHP应该返回如下数据:
<?php
header("Content-Type: application/json");
die(json_encode($yourData));
?>
答案 1 :(得分:0)
您的模型getPrice()
方法必须返回某些内容。