简体   繁体   中英

Not able to get value from JSON object using AJAX call

I have sample code for getting json value from servlet through Ajax call. In success function I am getting response. It is showing Object : [{"userId":"dfeterter"}] in console.

But I am not able to get value of userId

$.ajax({
    url: "Registration",
    dataType: "json",
    data: {
        jsonbhvalue: bhvalue,
        jsonuid: uid,
        jsonpassword: password,
        jsonfname: fname,
        jsonlname: lname,
        jsonmobile: mobile,
        jsonemailid: emailid
    },
    success: function(variable) {
        var obj = $.parseJSON(JSON.stringify(variable));
        console.log("Object     :   " + obj);
        console.log("cval     :   " + obj.userId)
    });
});

Thanks To @RobertoNovelo.

You have to remove $.parseJSON as you are already setting JSON by ajax configuration. dataType: "json" You need to use:

obj[0].userId

Your response is array of objects.

That is because your object is an array. You should either use obj[0] or assign the array to a variable and then use its members

 objs = [{"userId":"dfeterter"}] firstobj = objs[0] console.log("Object : " + objs); console.log("cval : " + objs[0].userId); console.log("cval : " + firstobj.userId); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM