【发布时间】:2019-10-10 09:32:41
【问题描述】:
我无法理解如何通过获取 API 来引用我收到的对象。有
var data [{"id": "7150", "employee_name": "Julia", "employee_salary" = "39999"}]
在 JS 文件中,我可以很容易地引用一个变量,例如对它进行排序 (data.sort ((a, b))
但是.. 如果我的代码中有什么
return fetch (url).then (function (resp) {
return resp.json ()
}
.then (data => {
data.forEach (result => {
....
})
})
数据在“then”之外是不可见的吗? 如何在“then”之外引用我的 api 数据?
【问题讨论】:
-
你查询的是哪个
then的范围 -
data只是一个局部变量,在调用回调函数时可用。您可以将更大范围的变量设置为data以供以后使用。 -
此时您可以将
data保存为全局变量
标签: javascript json api object var