1

I don't receive an error, but the Javascript code just stops executing when I run the following function:

function initGoogleURL() {
    gapi.client.setApiKey(myAPIKey);
    gapi.client.load('urlshortener', 'v1').then(function(value) {
        console.log("success!");
        googleReady = true; // Success!
    }, function(reason) {
        console.log(reason); // Error!
    });
}

I don't see a message in the console. And the rest of the code stops executing, even though it's not dependent on this path of execution.

Any ideas why this is happening?

4

1 回答 1

0

首先 - 您在控制台中没有收到任何消息这一事实令人担忧。你确定你正在执行代码吗?你有线吗

initGoogleURL();

在你的代码的某个地方?假设您这样做,我认为该问题与此处描述的问题相似。我的理由是如果我在函数上设置超时,例如:

setTimeout(function(){
  initGoogleURL();
}, 5000) // Second argument is time to wait before executing the function

然后,我会打印出一条成功消息。如果您看到类似的行为,则您的问题是您在完全加载之前尝试使用 google api。对此的解决方法是为 API 库成功加载时定义一个回调处理程序,如 HTML 中的 import 语句中所述,例如:

<script src="https://apis.google.com/js/client.js?onload=OnLoadCallback"></script>
于 2015-11-24T03:03:30.057 回答