当我尝试从JSON响应中获取值时,我坚持了下来。这是我的代码
代码:
$.ajax({
url: 'checkvotes.php',
dataType: "json",
success: function(data) {
// want to fetch UP and DOWN variables from JSON here
}
});
来自PHP的AJAX响应
{"sample":[{"id":"1","message":"my message","up":"200","down":"34"}]}
答案 0 :(得分:6)
$.ajax({
url: 'checkvotes.php',
dataType: "json",
success: function(data) {
var up = data.sample[0].up;
var down = data.sample[0].down;
}
});
答案 1 :(得分:1)
尝试data.sample[0].up
和data.sample[0].down
。如果有疑问,请使用此JavaScript来模拟调用:
var data = {"sample":[{"id":"1","message":"my message","up":"200","down":"34"}]};
在调试器中运行它并检查data
。
答案 2 :(得分:0)
var up = data['sample'][0]['up'],
down = data['sample'][0]['down']
只需打印一个console.log(数据)来检查你的json