未定义gap与requirejs一起使用时会出现错误
gapi is not defined error comes when using with requirejs
我正在尝试加载带有requireJS的google API文件,但收到的错误为,有人认为我可以轻描淡写地说,GP文件是在谷歌调用之前加载的
"gp.js:23未捕获引用错误:未定义漏洞"
这是的代码
gp.js文件
function logout()
{
gapi.auth.signOut();
location.reload();
}
function login()
{
var myParams = {
'clientid' : '900278902057-ppqm358qrhki089danipqguj3i4ir70i.apps.googleusercontent.com',
'cookiepolicy' : 'single_host_origin',
'callback' : 'loginCallback',
'approvalprompt':'force',
'scope' : 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
};
gapi.auth.signIn(myParams);
}
function loginCallback(result)
{
if(result['status']['signed_in'])
{
var request = gapi.client.plus.people.get(
{
'userId': 'me'
});
request.execute(function (resp)
{
var email = '';
if(resp['emails'])
{
for(i = 0; i < resp['emails'].length; i++)
{
if(resp['emails'][i]['type'] == 'account')
{
email = resp['emails'][i]['value'];
}
}
}
var str = "Name:" + resp['displayName'] + "<br>";
// str += "Image:" + resp['image']['url'] + "<br>";
// str += "<img src='" + resp['image']['url'] + "' /><br>";
// str += "URL:" + resp['url'] + "<br>";
str += "Email:" + email + "<br>";
str += "DOB:" + resp['birthday'] + "<br>";
str += "Gender:" + resp['gender'] + "<br>";
document.getElementById("profile").innerHTML = str;
});
}
}
function onLoadCallback()
{
gapi.client.setApiKey('AIzaSyBy08qpAjR9U1nKaZ5H1MmwTuthspQPNqY');
gapi.client.load('plus', 'v1',function(){});
}
以及requirejs文件-main.js
require.config({
shim: {
'jquery': {
exports: '$'
},
/* 'backbone': {
deps: ['jquery', 'underscore'],
},*/
'googleplus' : {
deps: ['jquery'],
exports: 'gapi'
},
},
paths: {
'jquery': '//code.jquery.com/jquery-1.11.0.min',
'googleplus': 'https://apis.google.com/js/plus.js?onload=init',
}
})
require(['gp']);
和html按钮
<input type="button" value="Login" onclick="login()" />
<input type="button" value="Logout" onclick="logout()" />
当我尝试不使用requireJS时,同样的代码也能很好地工作,但问题是我必须使用requireJS
gp.js必须是一个模块。
define(['jquery', 'googleplus'], function($, gapi) {
# your code
window.loginCallback = function (result) {
if (result['status']['signed_in']) {
gapi.client.load('plus', 'v1', function () {
var request = gapi.client.plus.people.get(
{
'userId': 'me'
});
request.execute(function (resp) {
var email = '';
if (resp['emails']) {
for (i = 0; i < resp['emails'].length; i++) {
if (resp['emails'][i]['type'] == 'account') {
email = resp['emails'][i]['value'];
}
}
}
var str = "Name:" + resp['displayName'] + "<br>";
str += "Email:" + email + "<br>";
str += "DOB:" + resp['birthday'] + "<br>";
str += "Gender:" + resp['gender'] + "<br>";
document.getElementById("profile").innerHTML = str;
});
});
}
};
$('#login').click(login);
$('#logout').click(logout);
});
并修改模板:
<input type="button" value="Login" id="login" />
<input type="button" value="Logout" id="logout" />
因为gapi.signIn
方法需要全局命名空间中的回调函数,所以loginCallback
函数必须是全局的。
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 我如何修复包含在captcha的addthis中的错误
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- CKFinder 3为所选文件返回错误的URL
- 同位素库错误:未捕获错误无布局模式包装生产线8
- 铬:“;未捕获的语法错误:意外的标记:"
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 相位器状态未捕获参考错误
- /undefined在我的404错误日志中多次出现
- Javascript未捕获语法错误意外的标识符错误
- 我能处理来自gap.client.load'的错误吗;的承诺
- Google嵌入API cb=gap对象错误
- Angular http在Phone gap中返回错误,但在浏览器中成功
- 未定义gap与requirejs一起使用时会出现错误