javascript - 变量显示未定义
问题描述
我正在点击一个 api 并将响应保存在一个变量中,但是我存储的变量显示为调试器未定义。
try {
let response = await fetch(
"http://test.kelltontech.net/eventengine/getmyevents?hash=****×tamp=****&id=****",
{
method: "GET",
headers: {
"Content-Type": "application/json"
}
}
);
let responseJson = await response.json();
this.agendaDetails = await responseJson;
} catch (error) {
alert(error);
}
这里 responseJson 有数据,但没有存储在主类之外声明的议程详细信息中,如议程详细信息= {}
解决方案
您应该使用 fetch 和 .then() 在 json 中获取响应,然后将该响应分配给变量,如下所示
fetch(URL.apiUrlUploadImages, {
method: "POST",
headers: {
Accept: 'application/json',
'Authorization': 'Bearer ' + this.token,
'Content-Type': 'application/json',
},
body: JSON.stringify({
"front_image": this.frontImageData
})
})
.then((response) => response.json())
.then((responseData) => {
var result = responseData});
推荐阅读
- cloud-foundry - 如何将 PCF ASG 中的 slack web-hooks 域列入白名单?
- javascript - 是否可以在 Wicket 中进行平滑的页面导航转换?
- python - 选择除 BeautifulSoup 中某些类的所有 div
- android - 等待 RxJava 响应
- r - 如何从给定的坐标中检索位置/区域?
- python - pandas 遍历列行
- c++ - 错误 C2280 / 在 std::vector 中实例化对象时复制构造函数出现问题?
- tensorflow - TensorFlow2.0 会减少不必要的计算吗?
- azure-devops - 我们可以为多个组织设置一个自托管代理吗?
- android - 如何使用放大动画制作径向渐变动画?