【发布时间】:2018-10-24 02:02:41
【问题描述】:
我正在尝试使用 ajax 函数从通过 post 方法接收到的 .json 文件中的数据获取到外部服务器,并将其全部显示在 html 上(标签和数据)。 我可以获取 json 数据并通过控制台日志显示它,但是我无法对其进行迭代,或者只是在 html 端显示。
我尝试创建一个公共数组并将响应推送给它,创建一个函数等。但看起来 ajax 函数在它之后加载,并且根本没有数据,然后我尝试连接一个带有我在控制台上获得的值的字符串,但没有成功。
我什至无法获得实际数据,只有标签。我在这里有点困惑。有人能帮我吗?
这是我的代码以及它在控制台上显示的内容。
component.hmt:
<ul>
<li>{{res}}</li>
</ul>
component.ts
public res = "";
ngOnInit() {
let settings = {
"async": true,
"crossDomain": true,
"url": "ip/to/api",
"context": this, //added this
"method": "POST",
"data": "\"dataApi\"",
};
$.ajax(settings).done(function (response) {
console.log(response);
this.res = response; //added this
for(let i in response)
{
//console.log(i);
this.res += i + " - ";
for(let j in response[i])
{
//console.log(j);
this.res += j + " : \n ";
for(let t in response[i][j])
{
//console.log(t);
this.res += t +"\n";
}
}
}
console.log(this.res);
return this.res;
});
}
}
这是 console.log 上的输出: 问题是在控制台日志上我可以获取所有标签:数据,但我永远无法获取要显示的 res 字符串上的数据。
Console.log(response) : 我需要显示的所有数据。
this.res : 没有任何数据的标签。
提前致谢!!
编辑:这是 {{res | json}} 在 html 端:
{“挑战”:[{“挑战.Closed”:假, "Challenge.EndSubmissionDate": "2010 年 2 月 13 日星期六 00:00:00 GMT", “Challenge.Title”:“net.Partner”、“Challenge.CompositeId”: "Id=3_Tenant=103", "Challenge.Code": "2010/001"....
我有 json 之类的基于文本的..我想要例如:
Challenge.Closed : false
....
Idea.id: 4
...
并访问一些我知道存在的标签,比如 id.. 我必须映射它吗?
【问题讨论】:
标签: ajax angular typescript