When I tried to fetch the values from a JSON response, I stucked up. 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
{"sample":[{"id":"1","message":"my message","up":"200","down":"34"}]}
When I tried to fetch the values from a JSON response, I stucked up. 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
{"sample":[{"id":"1","message":"my message","up":"200","down":"34"}]}
Share
Improve this question
edited Jul 23, 2012 at 14:13
TheAlbear
5,5938 gold badges54 silver badges86 bronze badges
asked Oct 7, 2010 at 12:41
RajasekarRajasekar
19k35 gold badges109 silver badges140 bronze badges
2
- 1 alert(data.id); which says undefined – Rajasekar Commented Oct 7, 2010 at 12:45
- The object doesn't have an id property, it only has a sample property (which has a length 1 array as its value which… – Quentin Commented Oct 7, 2010 at 12:50
3 Answers
Reset to default 6$.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
. If in doubt, use this JavaScript to emulate the call:
var data = {"sample":[{"id":"1","message":"my message","up":"200","down":"34"}]};
Run that in a debugger and examine data
.
var up = data['sample'][0]['up'],
down = data['sample'][0]['down']
just print a console.log(data) to inspect your json
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744796671a4594244.html
评论列表(0条)