Javascript 是否将JSON响应保存到变量以便在其他函数中使用?
我试图将json响应保存到jsondata变量,以便在其他函数中使用。但它说它是未定义的Javascript 是否将JSON响应保存到变量以便在其他函数中使用?,javascript,json,fetch,Javascript,Json,Fetch,我试图将json响应保存到jsondata变量,以便在其他函数中使用。但它说它是未定义的 let jsondata; fetch( `https://yahoo-finance15.p.rapidapi.com/api/yahoo/op/option/AMD`, { method: 'GET', headers: { 'x-rapidapi-host': 'yahoo-finance15.p.rapidapi.com', 'x-rapidapi-
let jsondata;
fetch(
`https://yahoo-finance15.p.rapidapi.com/api/yahoo/op/option/AMD`,
{
method: 'GET',
headers: {
'x-rapidapi-host': 'yahoo-finance15.p.rapidapi.com',
'x-rapidapi-key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
},
}
)
.then(
function (u) { return u.json(); }
).then(
function (json) {
jsondata = json;
}
)
console.log(jsondata)
我认为fetch承诺返回的第一个参数是响应数据 所以 `让杰桑达塔
fetch(
`https://yahoo-finance15.p.rapidapi.com/api/yahoo/op/option/AMD`,
{
method: 'GET',
headers: {
'x-rapidapi-host': 'yahoo-finance15.p.rapidapi.com',
'x-rapidapi-key':
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
},
}
)
.then(
function (u) { jsondata = u }
)`
在函数中包装
fetch
,并将其放在return jsondata的最后一行代码>仍然返回未定义引用到我上面添加的副本!你会找到许多适合你需要的答案。如果未正确返回未定义的
,这是非常常见的。