简体   繁体   English

如何显示从jQuery返回的JSON

[英]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 如何从JSON值获取显示返回值。我需要获取用户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. 这些值以html形式获取。 In that I need to store user id in Value varable. 在那我需要将用户ID存储在Value变量中。 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 提前致谢

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 如果您知道记录长度为1,则直接使用

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

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. 尝试记录success是没有意义的,因为接收到的数据中没有任何success密钥。

Make sure that you get the response in proper json format,and as harshad pointed String male should be wrapped in double quotes. 确保以正确的json格式获得响应,并且如苛刻所指出的那样,应将String male用双引号引起来。

After you get that fixed,you can access the user_id as: 解决此问题后,您可以按以下方式访问user_id:

data[0].user_id

data.success is undefined, because the received data is stored directly in data . data.success未定义,因为接收到的数据直接存储在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.. 您可以通过data[0]直接访问接收到的数据,以将对象放入数组内部,或者,如果您有更大的数组,则可以对其进行每个循环,等等。

Try this, simply use json.parse() 试试这个,只需使用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> 

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;
  }
 });
});

There is a json error 发生json错误

"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. 您需要确保响应的格式正确且符合JSON.org标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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