Home » Javascript » Retrieving JSON data in Javascript

Retrieving JSON data in Javascript

Posted by: admin November 1, 2017 Leave a comment

Questions:

This is how I retrieve data from my database but I get an error when trying to map to the data. How am I suppose to do this?

$("button").click(function(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "/pages/test/",
        data: { 
            id: $(this).val(), // < note use of 'this' here
            access_token: $("#access_token").val() 
        },
        success: function(result) {
            var obj = JSON.parse(result);
            //trying to map to map on the name in obj
            alert(obj.name);
            alert('ok');
        },
        error: function(result) {
            alert('error');
        }
    });
});

JSON

[
    {
        "id":1,
        "name":"Irish",
        "price":"10.00",
        "created_at":"2017-10-26 12:49:38",
        "updated_at":"2017-10-26 12:49:38"
    },
    {
        "id":2,
        "name":"Salad",
        "price":"10.00",
        "created_at":"2017-10-26 12:49:49",
        "updated_at":"2017-10-26 12:49:49"
    }
]
Answers: