javascript - 将响应传递给 addEventListener 中的函数
问题描述
我正在使用一个 API,其中响应来自 addEventListener 函数,我想在收到响应后将其传递给函数。我正在努力实现以下目标:
getToken() {
var data = JSON.stringify({
"app_key": "my_app_key",
"app_secret": "my_app_secret"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", async function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
await val = this.responseText;
this.recieveApiResponse(val);
}
});
xhr.open("POST", "https://tokenized.sandbox........../grant");
xhr.setRequestHeader("username", "my_username");
xhr.setRequestHeader("password", "my_password");
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(data);
}
recieveApiResponse(resp) {
console.log(resp)
}
解决方案
推荐阅读
- laravel - Laravel 查询生成器加入 3 个表并计算结果
- mongodb - mongodb:选择嵌入式数组中的最新条目
- java - 显示最大可除数而不是所有数字
- javascript - LinkedIn分享按钮重定向到主页?
- flutter - Image Picker Plugin Flutter 确认界面
- meld - 如何在三向合并选项卡仍打开时手动将文件标记为已解决
- selenium - 如何区分硒/黄瓜参数化数据测试的测试描述?
- android - 如何从android studio中的商家应用程序向客户端应用程序发送推送通知?
- asp.net-mvc - MVC 报告查看器呈现/响应问题
- react-native - 错误:GoogleSignIn.null:DEVELOPER_ERROR 在 expo-google-sign-in