简体   繁体   English

使用Google Closure进行Ajax调用

[英]Ajax call using Google Closure

I am new to Google Closure library, and I am trying to simulate something similar to that of Jquery's $.ajax function. 我是Google Closure库的新手,我正在尝试模拟类似于Jquery的$ .ajax函数的东西。 Here is what I tried and what I got in response. 这是我尝试过的以及得到的回应。

The trigger is from Chrome Extensions Right click 触发来自Chrome扩展程序,右键单击

chrome.contextMenus.create({"title": "sample_closure", "onclick": samp.myProject.fun1,"contexts":['selection']});

This triggers the fun1 function which is defined as below: 这将触发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);
}

The getAjaxData function is as below. getAjaxData函数如下。

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. 但是,当我调用getAjaxData函数时出现此错误。

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. 谁能告诉我我要去哪里错..我检查了需要传递给xhrio.send函数的Argument类型,它具有字符串类型。

Solved this issue.. The path to base.js is causing this problem. 解决了此问题。base.js的路径导致了此问题。

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 因此,请检查您到闭包库的base.js的路径以解决此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM