I’ve gotten this json object back from django.
I was wondering how do we use ajax/jquery to retrieve just the words that are in bold?
Basically i would like to retrieve the pair of the key – “keyword”
I’ve tried various methods below but to no avail either getting a undefined or the whole chunk below.
I’m stumped.
{ "new_list" : { "a1" : { "exp_datetime" : "2012-03-07 22:13:31",
"keyword" : "**a1**",
"sub_datetime" : "2012-02-29 22:13:31"
},
"blink182" : { "exp_datetime" : "2012-03-07 22:12:40",
"keyword" : "**blink182**",
"sub_datetime" : "2012-02-29 22:12:40"
},
"blue" : { "exp_datetime" : "2012-03-07 22:14:44",
"keyword" : "**blue**",
"sub_datetime" : "2012-02-29 22:14:44"
},
"jolie" : { "exp_datetime" : "2012-03-07 22:08:46",
"keyword" : "**jolie**",
"sub_datetime" : "2012-02-29 22:08:46"
},
"santa claus" : { "exp_datetime" : "2012-03-07 22:14:13",
"keyword" : "santa claus",
"sub_datetime" : "2012-02-29 22:14:13"
},
"teleport" : { "exp_datetime" : "2012-03-07 22:09:26",
"keyword" : "**teleport**",
"sub_datetime" : "2012-02-29 22:09:26"
}
} }
Here are the methods i’ve tried:
function updateKeywords(e) { e.preventDefault();
var keyword_form = jQuery(e.target);
alert("Yay Jquery is working!!!");
$.ajax({
url : keyword_form.attr('action'),
type : keyword_form.attr('method'),
data : keyword_form.serialize(),
dataType : 'json',
success : function(response) {alert("JSON Data: " + response.new_list.keyword);
},
});
}
});
Unfortunately, when i put the above, it returns an undefined json data.
When i just put response.new_list, it returns the whole chunk.
Do appreciate any help.
In your success callback function, you can loop through the keys of the
new_listobject like so, accumulating a list of keywords as you go:See jsFiddle.