How to loop a json array and update links
On page load, I do one call to get the current status of all the favourite links (display the right message aka: click to subscribe, click to unsubscribe.
Final Code! :) Thanks for your help guys, please check post revisions to see the issue.
$(InitFavorite);
function InitFavorite(){
var jList = $(".favourite_link");
var ids_to_check = {};//new Array();
$.each(jList, function () {
var id = this.id;
var object = id.split("_");
if (!ids_to_check[object[1]]) {
ids_to_check[object[1]] = [];
}
ids_to_check[object[1]].push(object[0]);
});
$.ajax({
type: 'POST',
url: '/user/subscription/favourite-listing',
data: ids_to_check,
dataType: 'json',
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application开发者_StackOverflow/json;charset=UTF-8");
}
},
error: function() {
//could not load favourites
},
success: function(returned_values) {
$.each(returned_values.favourites.Clip, function(i, item) {
$('#'+i+'_Clip').html(''+item+'');
});
$.each(returned_values.favourites.Playlist, function(i, item) {
$('#'+i+'_Playlist').html(''+item+'');
});
}
});
You must parse your text so it becomes an object instead of JSON (plain-text):
var o = eval('(' + returned_values + ')');
then you should be able to use o
as the object:
alert(o.env); // Gives "development"
more specifically:
success: function(returned_values) {
var o = eval('(' + returned_values + ')');
$.each(o, function(i, item) {
console.log(item);
});
}
精彩评论