为什么此函数返回响应为 null
编写了一个关于事件点击的函数,如下所示
$('#ClickMe').live('click', function () {
chrome.extension.sendRequest({ method: "getT" }, function (response) {
alert(response.data); // Displaying undefined..
});
});
我在后台页面
function wish(){
return "Hey...";
}
chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
if (request.method == "getT"){
sendResponse({data: wish()});
}
else
sendResponse({});
});
。我无法在内容脚本中获得响应。请帮助我。
I wrote a function on event click as below
$('#ClickMe').live('click', function () {
chrome.extension.sendRequest({ method: "getT" }, function (response) {
alert(response.data); // Displaying undefined..
});
});
in background page..
function wish(){
return "Hey...";
}
chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
if (request.method == "getT"){
sendResponse({data: wish()});
}
else
sendResponse({});
});
I cannot get the response in Content Script.Please help me on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在 JSON 对象中发送类似的功能参数,您需要先实例化它,然后将其作为变量而不是函数传递,因为它会将其视为闭包,因此当它进行序列化时,它不会包括那。
上面的代码片段应该可以工作。
You cannot send functional parameters like that within a JSON object you would need to instantiate it first, and then pass it as a variable not as a function because it will treat it as a Closure, so when it does the serialization, it will not include that.
The above snippet should work.