Reputation: 2322
I'm trying to do something really easy: Select JSON data by its key but somehow it doesn't work.
This is my jQuery function:
$.ajax({
url: "/_add_question",
data: {
title: function() {
return title.val();
},
text: function() {
return text.val();
},
slide_id: function() {
return aside.attr('id');
},
},
success: function(data) {
aside.append("<h3>" + data.title + "</h3>");
aside.append("<p>" + data.text + "</p>");
}
});
This is what my server returns:
{"text": "b", "title": "a"}
and the data in the chrome debugger looks like this:
data: "{"text": "b", "title": "a"}"
But it keeps saying the data.title/data.text are undefined. (I also tried data['title'] and data[title])
Upvotes: 2
Views: 188
Reputation: 819
use dataType: 'json', in ajax call this may solve your problem
Upvotes: 1