【发布时间】:2019-01-25 20:03:46
【问题描述】:
为什么 sendResponse({ helloWorld: sendto }) 发回空字符串而不是 this.responseText 分配给 sendto 变量?如何正确管理我的代码以实现返回 this.responseText 而不是空字符串?
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
**var sendto = 'empty';**
var ccc = new XMLHttpRequest();
ccc.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText)
**sendto = this.responseText;**
}
}
ccc.open("GET", "http://127.0.0.1:3000/" + request.greeting)
ccc.send();
**sendResponse({ helloWorld: sendto });**
});
【问题讨论】:
-
在监听函数末尾添加
return true;,以提供异步响应。 -
它不会工作......
-
尝试发布的答案
标签: javascript google-chrome-extension xmlhttprequest