简体   繁体   English

从$ .post()检索json数组

[英]retrieve json array from $.post()

<script>
$(document).ready(function(){
  $("#asd").click(function(){
    $.post("/sister/rest/rest/login",
    {
      email:"t@s.com",
      pwd:11
    },
    function(data){
      $('#id3').append(data.user_fullname);
    });
  });
});

</script>

when i cliked the #asd, #id3 shows "ch", the json array from /sister/rest/rest/login [{"user_id":"1","user_fullname":"ch","user_email":"t@s.com"}] 当我喜欢#asd时,#id3显示“ ch”,来自/sister/rest/rest/login [{"user_id":"1","user_fullname":"ch","user_email":"t@s.com"}]的json数组[{"user_id":"1","user_fullname":"ch","user_email":"t@s.com"}]

when i tried the $('#id3').append(data[0].user_fullname); 当我尝试$('#id3').append(data[0].user_fullname); it showed nothing :(, why? 它什么也没显示:(,为什么?

im newbie in getting json data, so please help me! 我是新手,正在获取json数据,所以请帮助我! Thank you all 谢谢你们

You need to parse the JSON object before trying to pull out the elements. 在尝试提取元素之前,您需要解析JSON对象。 You do this by using JSON.parse() 您可以使用JSON.parse()完成此操作

<script>

$(document).ready(function(){
  $("#asd").click(function(){
    $.post("/sister/rest/rest/login",
    {
      email:"t@s.com",
      pwd:11
    },
    function(data){
      data = JSON.parse(data);
      $('#id3').append(data.user_fullname);
    });
  });
});

</script>
[{"user_id":"1","user_fullname":"ch","user_email":"t@s.com"}

Looks like you are missing a closing tag. 好像您缺少结束标记。

[{"user_id":"1","user_fullname":"ch","user_email":"t@s.com"}]

I use JSONLint to quickly validate JSON and determine where is the issue. 我使用JSONLint快速验证JSON并确定问题出在哪里。

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

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