【发布时间】:2019-07-07 17:20:35
【问题描述】:
我在运行console.log 时可以看到JSON,我认为问题与我的return 语句有关。
JS:
import $ from 'jquery';
import headlineData from '../JSON/headline.json';
export default class {
constructor() {
this.loadHeadlineData();
}
// ------------------- //
loadHeadlineData() {
let res = headlineData.d.results.map(function(obj) {
return {
"Name": obj.preferredname,
"Initials": obj.initials,
"Title": obj.jobtitle,
"Office": obj.office
}
})
$("#headline-cont h1").append(res);
}
}
console.log(headlineData)
JSON:
{
"d": {
"results": [{
"preferredname": "Bobson A. Dugnutt",
"initials": "BAD",
"jobtitle": "Coolguy",
"office": "New York"
}]
}
}
HTML sn-p:
<div id="headline-cont">
<h1></h1>
</div>
console.log(JSON.stringify(headlineData.d.results)):
[{"preferredname":"Bobson A. Dugnutt","initials":"BAD","jobtitle":"Coolguy","office":"New York"}]
【问题讨论】:
-
尝试使用
JSON.stringify(res) -
@MaheerAli 没什么,很遗憾:\
-
@Jeto 很高兴知道,谢谢。我更新了我的代码(仍然没有渲染)
-
@Jeto 我将 HTML 和控制台结果添加到问题正文中。
-
你能显示
console.log($("#headline-cont h1"));的结果吗?另外,也许在导入后尝试window.$ = $,不确定是否需要这样做,因为我从不在那种上下文中使用 jQuery。
标签: javascript jquery json append