【问题标题】:Json object display in htmlJson对象在html中显示
【发布时间】:2017-02-03 22:48:46
【问题描述】:

大家好,我是这种 json 语法的新手,但我遇到了一个问题。 我制作了从 json 中的远程服务器获取数据的函数,如下所示:

<!DOCTYPE html>
<html>
<head>

  <script type="text/javascript" src="jquery-3.1.0.min.js"></script>
  <script type="text/javascript">

    function getStates(value) {

            var data = {
                q: value
            } 

            $.ajax({
                url: 'https://something.hr/api/search',
                method: 'GET',
                headers: {'Accept': 'application/json'},
                data: data
            }).done(function(data) {
                //do something with data I returned to you
                console.log("success")
                console.log(data)


            }).fail(function(data) {
                //doSomethingWith the data I returned to you
                console.log("fail")
                console.log(data)
            });
        };



  </script>
</head>
<body>
    <input type="text" onkeyup="getStates(this.value)" >
    <br>

 <script>
</script>

</body>
</html>

我的问题是我知道如何在控制台日志中获取对象,但我希望在 html 中获取该对象,例如在某些框中,而不是单击它并打开他的数据(姓名、ID、地址等)

【问题讨论】:

  • JSON.stringify(data,null,4) 会将您的 json 返回到偏移量为 4 的漂亮字符串中。然后您可以将其插入您选择的元素中。
  • 你能提供你的 JSON 样本吗?
  • jsfiddle.net/pfbd8ke6 这是我在控制台日志中的结果,首先我得到对象,当我打开它时,我得到了信息。所以现在我希望以一种很好的方式对 iformatios 进行排序
  • SON.stringify(data,null,4) 是个好主意。我用过。现在我遇到了不同的问题。列表不会消失。

标签: javascript jquery html json ionic-framework


【解决方案1】:

var httpManager = (function() {
  var getData = function(value) {
    $.ajax({
      url: 'https://api.github.com/users/zixxtrth',
      method: 'GET'

    }).done(function(data) {
      //do something with data I returned to you
      jQuery('#avatar').attr('src', data.avatar_url);
      jQuery('#name').html(data.name);
      jQuery('#username').html(data.login);

    }).fail(function(data) {
      //doSomethingWith the data I returned to you
      alert('No Data');
    });
  };

  return {
    getData: getData()
  }

})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<img src='' id='avatar' width='200' height='200' />
<h1 id='name'></h1>
<h2 id='username'></h2>

【讨论】:

    猜你喜欢
    • 2020-06-04
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    相关资源
    最近更新 更多