$('select[name=\'service_id\']').bind('change', function() {
service_id = $('select[name=\'service_id\']').val();
$.ajax({
url: 'myajaxfile.php&service_id=' + service_id,
dataType : 'json',
success: function(json) {
var max_use = json.max_use;
var used = json.used;
var amount = json.amount;
$('input[name=\'max_use\']').val(max_use);
$('input[name=\'used\']').val(amount);
}
});
});
Above is my piece of code. All I want is to bind the value from json result which is ing fine to two or more text boxes which is not happening. The json result is like [{"max_use":"0","period":"30","amount":"99"}]
which is very correct. On doing an alert to amount it says undefined. Would be great help if point me out what's the problem is? I tried searching on stackoverflow but found no perfect solution that works. Thanks.
$('select[name=\'service_id\']').bind('change', function() {
service_id = $('select[name=\'service_id\']').val();
$.ajax({
url: 'myajaxfile.php&service_id=' + service_id,
dataType : 'json',
success: function(json) {
var max_use = json.max_use;
var used = json.used;
var amount = json.amount;
$('input[name=\'max_use\']').val(max_use);
$('input[name=\'used\']').val(amount);
}
});
});
Above is my piece of code. All I want is to bind the value from json result which is ing fine to two or more text boxes which is not happening. The json result is like [{"max_use":"0","period":"30","amount":"99"}]
which is very correct. On doing an alert to amount it says undefined. Would be great help if point me out what's the problem is? I tried searching on stackoverflow but found no perfect solution that works. Thanks.
- would you please help me for that please? – Preeti Maurya Commented Apr 23, 2015 at 9:04
3 Answers
Reset to default 4try
var t = JSON.parse(json);
var max_use = t[0]["max_use"];
var used = t[0]["used"];
var amount = t[0]["amount"];
Your json is an array with an object in it. So you must either change json, or access object properties like that:
var max_use = json[0].max_use;
var used = json[0].used;
var amount = json[0].amount;
proper json in your case would look like that:
{"max_use":"0","period":"30","amount":"99"}
without []
result is an array, you have value at 0 index, so use as below
result = [{"max_use":"0","period":"30","amount":"99"}][0]
fetch as below
result.max_user
result.period
result.amount
or
result["max_user"]
result["period"]
result["amount"]