【问题标题】:Getting a SyntaxError when loading API加载 API 时出现 SyntaxError
【发布时间】:2019-08-19 13:58:00
【问题描述】:

我像这样在 html 文档的头部加载 kongregate_api 脚本

<script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>

然后加载包含这些行的 main.js 脚本

    console.log(window.kongregateAPI)
    window.kongregateAPI.loadAPI(function () {});

错误信息;

SyntaxError:指定了无效或非法的字符串 在 kongregate_api.js:36

console.log 返回和对象,所以我认为它成功加载了脚本。

【问题讨论】:

    标签: javascript api kongregate


    【解决方案1】:

    加载 API

    为了加载 Kongregate API 对象,您需要包含一个脚本标签来加载我们的 JavaScript 源文件。 script 标签应该放在文档的 head 部分。

    HTML

    <script src='https://cdn1.kongregate.com/javascripts/kongregate_api.js'></script>
    

    初始化 API

    JavaScript API 会自动创建一个名为 kongregateAPI 的全局变量。您可以使用此对象通过 loadAPI 和 getAPI 函数初始化 Kongregate API 服务:

    示例:加载 API,并在初始化后设置对名为 kongregate 函数的 API 对象的全局引用。

    JAVASCRIPT

    kongregateAPI.loadAPI(function(){
      window.kongregate = kongregateAPI.getAPI();
      // You can now access the Kongregate API with:
      // kongregate.services.getUsername(), etc
      // Proceed with loading your game...
    });
    

    注意 每次 Kongregate 游戏页面加载时,API 应该只加载/初始化一次。如果您的应用程序跨越多个页面/文档,请参阅 Kongregate Shell 上的文档以获取有关设置和访问持久 API 连接的信息

    【讨论】:

    • 这正是kongregate javascript api页面中描述的方式。但我仍然无法在我的代码中看到问题。 Kongregate JS API Page
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多