arnoutaertgeerts
arnoutaertgeerts

Reputation: 2322

JSON select data

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

Answers (2)

Sri Tirupathi Raju
Sri Tirupathi Raju

Reputation: 819

use dataType: 'json', in ajax call this may solve your problem

Upvotes: 1

moonwave99
moonwave99

Reputation: 22817

With all chance, your response is not parsed, use $.getJSON.

Upvotes: 1

Related Questions