Chrome 扩展 - 在“onclick”回调函数中创建一个 javascript XMLHttpRequest
我有以下问题: 我正在尝试为使用上下文菜单的 Chrome 扩展编写 JavaScript 代码。
var id = chrome.contextMenus.create({"title": "search Flickr", "contexts":"selection","onclick":searchSelection});
function searchSelection(info,tab){
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "sumGet.phtml?figure1=5&figure2=10", false);
xhReq.send(null);
var serverResponse = xhReq.responseText;
alert(serverResponse); // Shows "15"
}
正如你所看到的,我正在尝试在此函数中创建一个 http 请求。由于某种原因,这不起作用。 怎么了? 谢谢, 玛丽
I have the following question:
I am trying to write a javascript code for a chrome extension that uses context menus.
var id = chrome.contextMenus.create({"title": "search Flickr", "contexts":"selection","onclick":searchSelection});
function searchSelection(info,tab){
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "sumGet.phtml?figure1=5&figure2=10", false);
xhReq.send(null);
var serverResponse = xhReq.responseText;
alert(serverResponse); // Shows "15"
}
as you can see I am trying to create an http request at this function. for some reason this doesn't work.
what is wrong?
Thanks,
Mary
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好使用异步
XMLHttpRequest
,同步调用会阻塞浏览器,这可能会导致糟糕的用户体验。对于异步请求,您必须使用回调处理程序,因为没有它您将无法获取
responseText
。当你做这样的事情时它会起作用吗:It's always better to use an asynchronous
XMLHttpRequest
, a synchronous call will block the browser, which might lead to a bad user experience.With an asynchronous request you'll have to use a callback handler, because without it you won't be able to get the
responseText
. Does it work when you do something like this: