我是 Google Closure 库的新手,我正在尝试模拟类似于 Jquery 的 $.ajax 函数的东西。这是我尝试过的以及我得到的回应。
触发器来自 Chrome Extensions 右键单击
chrome.contextMenus.create({"title": "sample_closure", "onclick": samp.myProject.fun1,"contexts":['selection']});
这会触发 fun1 函数,定义如下:
samp.myProject.fun1 = function(info,tab) {
var string_url = info.selectionText;
//String_url works fine and passed to the function below.
samp.myProject.getAjaxData(string_url);
}
getAjaxData 函数如下。
goog.require("goog.net.XhrIo");
samp.myProject.getAjaxData = function(url) {
goog.net.XhrIo.send(url, function(event) {
alert(event.target.getResponseText());
});
}
但是当我调用 getAjaxData 函数时出现此错误。
Error in event handler for 'contextMenus': TypeError: Cannot read property 'XhrIo' of undefined
谁能告诉我哪里出错了。我检查了需要为 xhrio.send 函数传递的参数类型,它具有字符串类型。