【发布时间】: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