Harsh Kanakhara
Harsh Kanakhara

Reputation: 1153

jQuery.parseJSON not working on Ajax Success

i am trying to parse json data which i get in response but using jQuery.parseJSON it is not working. Ajax Success not Working if i remove the below data from comment in Ajax Success Callback Function

$(document).ready(function(){
    //Working Example in Comment
    //var obj = jQuery.parseJSON( '{ "name": "harsh","address":["add1","add2"] }' );
    //var objArray = obj.address;
    //alert(objArray[2]);

    $('#getData').click(function(){
            $.ajax({
                url:'JsonServlet',
                type:'post',
                dataType: 'json',
                success: function(data) {
                    //var JSONdata = jQuery.parseJSON(data);
                    //alert(JSONdata);

                    $('#name').val(data.name);
                    $('#email').val(data.email);
                    $("#add").val(data.innerJSONObj1.address);
                }
            });
    });
});

Upvotes: 1

Views: 2401

Answers (1)

NSF
NSF

Reputation: 2549

since you specify dataType: 'json', the data variable is already a json object.

http://api.jquery.com/jquery.ajax/

If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. The parsed JSON object is made available through the responseJSON property of the jqXHR object.

Upvotes: 4

Related Questions