Created
January 13, 2014 02:10
-
-
Save jasti/8393578 to your computer and use it in GitHub Desktop.
Interpreting a Jquery response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assuming your server side script doesn't set the proper Content-Type: application/json response header you will need to indicate to jQuery that this is JSON by using the dataType: 'json' parameter. | |
Then you could use the $.each() function to loop through the data: | |
$.ajax({ | |
type: 'GET', | |
url: 'http://example/functions.php', | |
data: { get_param: 'value' }, | |
dataType: 'json', | |
success: function (data) { | |
$.each(data, function(index, element) { | |
$('body').append($('<div>', { | |
text: element.name | |
})); | |
}); | |
} | |
}); | |
or use the $.getJSON method: | |
$.getJSON('/functions.php', { get_param: 'value' }, function(data) { | |
$.each(data, function(index, element) { | |
$('body').append($('<div>', { | |
text: element.name | |
})); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment