'.done is successful but showing empty data
Once the quantity dropdown button is selected, the price field would be changed accordingly. However, I keep on getting successful alert, but the data is showing an empty result.
I have tried using success instead of done but the result is the same. I have also removed datatype: json, still no changes.
$('#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");
})
});
Here is my model:
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;
}
And here is my controller:
public function getPrice(){
$p_id = $_GET['p_id'];
$qty = $_GET['qty_ag'];
$price = $this->users_model->getPrice($prpduct_id, $quantity);
json_ok($price);
}
Now, the alert box is returning: Success{"status":"OK","data":{"pv_value":"120.00"}}
All I want to get is the value 120.00
Solution 1:[1]
Your model getPrice() method must return something.
Solution 2:[2]
You said you removed dataType: "JSON", but what type of data you're returning in the server side (PHP), and also, how you're returning it?
For example
If you're expectating JSON type of data, then you can still using this in 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");
})
});
But, in the server side, PHP should return data like this:
<?php
header("Content-Type: application/json");
die(json_encode($yourData));
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
