Anandh Sp
Anandh Sp

Reputation: 787

How to display returned JSON from a jQuery

How to get display return values from the JSON values.I need to get value the user id

 $('User_id').observe('blur', function(e) {
   var txt = $('User_id').value;
   jQuery.ajax({
     type: 'get',
     url: BASE_URL + 'admin/index/user_id',
     data: {
       user_id: txt
     },
     dataType: 'json',
     success: function(data) {
       console.log('success' + data.success);
       if (data.success) {
         var Value = data.location.user_id;
         alert(Value);
       }
     }
   });
 });

These values are getting in html form. In that I need to store user id in Value varable. But I receive successundefined as a output..

[{
  "user_id": "139",
  "mobile": "9042843911",
  "gender": "male",
  "hashcode": "DfAbMqLApAV6nVa1z940",
  "username": "anandhsp21",
  "password": "74bcff7d1199012e154f364e3f65e31d:8I",
  "authorized_person": "Anandh",
  "created": "2015-06-08 13:46:55",
  "modified": "2015-06-08 06:43:35",
  "logdate": "2015-06-08 08:16:55",
  "lognum": "12",
  "reload_acl_flag": "0",
  "is_active": "1",
  "extra": "N;",
  "rp_token": null,
  "rp_token_created_at": null,
  "app_name": "",
  "api_key": ""
}] 

Please some one help. Thanks in Advance

Upvotes: 0

Views: 2104

Answers (7)

Twinkle
Twinkle

Reputation: 111

To get userid please follow below code I edited,

$('User_id').observe('blur', function(e) {
   var txt = $('User_id').value;
   jQuery.ajax({
   type: 'get',
   url: BASE_URL + 'admin/index/user_id',
   data: {
     user_id: txt
   },
   dataType: 'json',
   success: function(data) {
     // this below userid is the value user_id you want.
     var userid = data[0].user_id;
  }
 });
});

Upvotes: 1

user786
user786

Reputation: 4364

Try this, simply use json.parse()

$(document).ready(function() {
  var v = ['{"user_id":"139","mobile":"9042843911"}'];
  var obj = JSON.parse(v);
  alert(obj.user_id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

vamsikrishnamannem
vamsikrishnamannem

Reputation: 4847

Your get the data in array so use loop in success data

for (var i=0; i<data.length; i++) {
    console.log('success' + data[i].user_id );
}

If you know the record length is 1 then use directly

 console.log('success' + data[0].user_id );

Upvotes: 2

Dropout
Dropout

Reputation: 13866

data.success is undefined, because the received data is stored directly in data. That's the first argument of the success block. You can access the received data directly by data[0] to get the object inside of the array, or if you have a larger array you can do a for each loop over it, etc..

Upvotes: 1

amit_183
amit_183

Reputation: 981

Make sure that you get the response in proper json format,and as harshad pointed String male should be wrapped in double quotes.

After you get that fixed,you can access the user_id as:

data[0].user_id

Upvotes: 1

Jeremy Thille
Jeremy Thille

Reputation: 26370

Your data is an array that contains one object. So you can access this object using :

success: function(data){
    console.log('success' + data[0].user_id );

Trying to log success is pointless, because there is no success key whatsoever in the received data.

Upvotes: 1

Harshad Vekariya
Harshad Vekariya

Reputation: 1052

There is a json error

"gender":male

Strings male should be wrapped in double quotes.

you need to make sure that your response is formatted appropriately and according JSON.org standards.

Upvotes: 0

Related Questions