如何将 JQuery 实例方法作为 Google API 回调发送?

发布于 2024-12-05 13:34:30 字数 1369 浏览 6 评论 0原文

以前有人知道如何发送(JQuery 插件)对象范围的方法作为 Google API 回调吗?例如,用于 PageSpeed API。

所有示例都显示您发送全局函数的名称,但我想以某种方式发送特定的实例方法(或重新查找 JQuery 对象的方法)。

这是基于他们的示例

 var s = document.createElement('script');
 s.type = 'text/javascript';
 s.async = true;
 var query = [
   'url=' + url,
   'callback=runCallbacks',
   'key=' + settings.api_key,
 ].join('&');
 s.src = API_URL + query;
 document.head.insertBefore(s, null);

所以我想实现的是像这样的调用:

 $('div.jq-google-plugin).MyGooglePlugin('callback',result);

我正在插件的 init 方法中设置对 Google 的调用。

例如,我尝试了这个 - 但这会产生来自 Google API 的 400 Bad Request。

 var query = [
   'url=' + url,
   'callback='+ encodeURI('function(result){$(\'div.jq-google-plugin\').MyGooglePlugin(\'callback\',result);}’,
   'key=' + settings.api_key,
 ].join('&');

我真正不想做的是创建一些名称混乱的全局只是为了捕获每个插件实例的响应,但我认为这是唯一的方法? (顺便说一句,这有效。)

 var query = [
   'url=' + url,
   'callback=myUniqueGlobalCallback',
   'key=' + settings.api_key,
 ].join('&');
 myUniqueGlobalCallback = function(result){$('div.jq-google-plugin).MyGooglePlugin('callback',result);}

提前感谢您的任何见解!我想这个解决方案可能不是特定于 Google API 的,但可能是围绕为什么它拒绝全局函数名称以外的任何东西。

Has anyone previously figured out how to send a (JQuery plugin) object-scoped method as a Google API callback? E.g. for the PageSpeed API.

All the samples show you sending the name of a global function, but I want to somehow send the specific instance method, (or a way to re-look up the JQuery object).

This is based on their sample:

 var s = document.createElement('script');
 s.type = 'text/javascript';
 s.async = true;
 var query = [
   'url=' + url,
   'callback=runCallbacks',
   'key=' + settings.api_key,
 ].join('&');
 s.src = API_URL + query;
 document.head.insertBefore(s, null);

So I want to achieve is something like this call:

 $('div.jq-google-plugin).MyGooglePlugin('callback',result);

I am setting up the call to Google from within my plugin's init method.

e.g. I tried this - but this produces a 400 Bad Request from Google API.

 var query = [
   'url=' + url,
   'callback='+ encodeURI('function(result){$(\'div.jq-google-plugin\').MyGooglePlugin(\'callback\',result);}’,
   'key=' + settings.api_key,
 ].join('&');

What I don’t really want to have to do is create some name-mangled global just to catch the responses for each plugin instance, but I think this is the only way? (This works BTW.)

 var query = [
   'url=' + url,
   'callback=myUniqueGlobalCallback',
   'key=' + settings.api_key,
 ].join('&');
 myUniqueGlobalCallback = function(result){$('div.jq-google-plugin).MyGooglePlugin('callback',result);}

Thanks in advance for any insights! I guess the solution may not be Google API-specific, but could be around why it rejects any thing other than a global function name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夢归不見 2024-12-12 13:34:30

jQuery 对 JSONP 有很好的支持。您应该使用它而不是自己注入脚本元素。这样您甚至不需要关心回调,响应将自动路由到您的 success 回调。

jQuery.fn.MyGooglePlugin = function() {
    $.ajax('<url>', {
        dataType: 'jsonp',
        data: {
            key: 'api-key'
        }
        success: function(response) {
            console.log(response);
        }
    });
};

jQuery has pretty good support for JSONP. You should use that instead of injecting the script element yourselves. With that you don't even need to care about the callback, and the response will automatically get routed to your success callback.

jQuery.fn.MyGooglePlugin = function() {
    $.ajax('<url>', {
        dataType: 'jsonp',
        data: {
            key: 'api-key'
        }
        success: function(response) {
            console.log(response);
        }
    });
};
拧巴小姐 2024-12-12 13:34:30

我真正不想做的是创建一些名称混乱的全局只是为了捕获每个插件实例的响应,但我认为这是唯一的方法?

这是唯一的办法。这里的问题是您正在做的是 JSONP 调用。这意味着您实际上并没有调用任何 javascript。您正在向页面动态添加新的脚本元素。该脚本元素将立即加载 url 中的 javascript,并且 javascript 将如下所示:

yourCallback({data: "Some Data"});

因为无论 yourCallback 是什么都必须是字符串才能工作,所以您不能传入对象。对于您想要做的事情有库支持,但在幕后,这就是它的工作方式,因为这是它唯一的工作方式。

What I don’t really want to have to do is create some name-mangled global just to catch the responses for each plugin instance, but I think this is the only way?

That's the only way. The problem here is that what you are doing is a JSONP call. That means you aren't actually calling any javascript. You are dynamically adding a new script element to the page. That script element will load the javascript in the url immediately and the javascript will look something like this:

yourCallback({data: "Some Data"});

Because whatever yourCallback is has to be a string for this to work, you can't pass in an object. There is library support for what you are trying to do, but behind the scenes, that's the way it works, because that's the only way it can work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文