使用 Google Closure 进行 Ajax 调用
我是 Google Closure 库的新手,我正在尝试模拟类似于 Jquery 的 $.ajax 函数的东西。这是我尝试过的以及得到的回应。
触发器来自 Chrome 扩展 右键单击
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 函数需要传递的参数类型,它具有字符串类型。
I am new to Google Closure library, and I am trying to simulate something similar to that of Jquery's $.ajax function. Here is what I tried and what I got in response.
The trigger is from Chrome Extensions Right click
chrome.contextMenus.create({"title": "sample_closure", "onclick": samp.myProject.fun1,"contexts":['selection']});
This triggers the fun1 function which is defined as below:
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);
}
The getAjaxData function is as below.
goog.require("goog.net.XhrIo");
samp.myProject.getAjaxData = function(url) {
goog.net.XhrIo.send(url, function(event) {
alert(event.target.getResponseText());
});
}
But I get this error when I call the getAjaxData function.
Error in event handler for 'contextMenus': TypeError: Cannot read property 'XhrIo' of undefined
Can anyone tell me where I am going wrong.. I checked the Argument type that need to be passed for xhrio.send function and it has string type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了这个问题。base.js 的路径导致了这个问题。
不删除这个问题,因为你们中的一些人可能会面临同样的问题并留给他们。 因此,请检查您的closure-library的base.js路径以解决此问题
Solved this issue.. The path to base.js is causing this problem.
Not deleting this question because some of you may face the same issue and leaving for them. So, Check your path to base.js of closure-library for solving this issue