为什么Google Plus Api中的'gapi.client'未定义? [英] Why is 'gapi.client' from Google Plus Api undefined ?

查看:310
本文介绍了为什么Google Plus Api中的'gapi.client'未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用google + api,并且我不得不修改样例认证示例以符合我的需求,如下所示:

 < script src =https://apis.google.com/js/client.js>< / script> 

取而代之:

 < script src =https://apis.google.com/js/client.js?onload=handleClientLoad>< / script> 

基本上删除了onload参数,并将所有功能放在了dom中。



这是我不明白的问题:

代码:

 的console.log(GAPI); 
console.log(gapi.client);

$ .each(gapi,function(){

console.log(this);

});

gapi.client.setApiKey(this.options.apiKey);

输出:


因此,我的问题基本上是:

p>

为什么在console.log(gapi)它显示它具有像client和auth这样的子对象,而在console.log(gapi.client)中它表示未定义?

解决方案

必须使用?onload回调参数,它在JS客户端异步完成加载时调用。通过在dom上运行,您正在尝试在定义之前访问gapi.client。发生什么事是/js/client.js脚本定义了gapi和一些辅助函数,但是直到JS客户端加载完成后才会定义gapi.client和gapi.auth。当你检查由console.log(gapi)记录的对象时,客户端已经完成加载,所以你看到gapi.client和gapi.auth被定义。


I am trying to use google+ api, and i had to modify the sample authentification example to fit my needs like this:

<script src="https://apis.google.com/js/client.js"></script>

Instead of this:

<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

Basically removed the onload parameter, and placed all the functionality on dom ready.

Here is the problem which i just don't understand:

Code:

console.log(gapi);
console.log(gapi.client);

$.each(gapi, function(){

        console.log(this);

});

gapi.client.setApiKey(this.options.apiKey);

Output:

So, my question basically is:

Why at console.log(gapi) it shows that it has sub-objects like client and auth, and at console.log(gapi.client) it says undefined ?

解决方案

You must use the ?onload callback parameter, it is called when the JS Client has finished loading asynchronously. By running on dom ready, you are trying to access gapi.client before it has been defined. What's happening is that the /js/client.js script defines gapi and some helper functions, but gapi.client and gapi.auth aren't defined until the JS client has finished loading. When you inspect the Object logged by console.log(gapi), the client has finished loading, so you see gapi.client and gapi.auth defined.

这篇关于为什么Google Plus Api中的'gapi.client'未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆