[英]How to retrieve JSON variables from a AJAX response
When I tried to fetch the values from a JSON response, I stucked up. 当我尝试从JSON响应中获取值时,我坚持了下来。 Here is my code
这是我的代码
Code: 码:
$.ajax({
url: 'checkvotes.php',
dataType: "json",
success: function(data) {
// want to fetch UP and DOWN variables from JSON here
}
});
AJAX Response from PHP PHP的AJAX响应
{"sample":[{"id":"1","message":"my message","up":"200","down":"34"}]}
$.ajax({
url: 'checkvotes.php',
dataType: "json",
success: function(data) {
var up = data.sample[0].up;
var down = data.sample[0].down;
}
});
Try data.sample[0].up
and data.sample[0].down
. 尝试
data.sample[0].up
和data.sample[0].down
。 If in doubt, use this JavaScript to emulate the call: 如有疑问,请使用以下JavaScript模拟调用:
var data = {"sample":[{"id":"1","message":"my message","up":"200","down":"34"}]};
Run that in a debugger and examine data
. 在调试器中运行它并检查
data
。
var up = data['sample'][0]['up'],
down = data['sample'][0]['down']
just print a console.log(data) to inspect your json 只需打印console.log(data)来检查您的json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.