【问题标题】:Uncaught TypeError when using Google API JavaScript Client使用 Google API JavaScript 客户端时未捕获的 TypeError
【发布时间】:2014-08-28 05:34:29
【问题描述】:

我正在使用云端点在 Google App 引擎上开发一个简单的井字游戏。我的 API 名称是 tictactoe2D。 我在 API 资源管理器中测试过的 API 中只有 1 个名为 tictactoe2D.compute2DMove() 的端点方法,并且工作得非常好。

现在我正在努力创建游戏的前端,并使用 Google APIs JavaScript 客户端库与我的端点方法进行通信。我遵循了与Getting Started 页面中显示的完全相同的过程,这是有关使用该库的完整教程。

这是 board.html 中的代码 sn-p,加载了 Javascript 库-

<head>
    <title>Tic Tac Toe 3D</title>
    <meta http-equiv="content-type" charset="utf-8" content="text/html"/>

    <link href="css/main.css" rel="stylesheet" type="text/css">

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://apis.google.com/js/client.js?onload=onLoadCallback"></script>

    <script type="text/javascript">
        function onLoadCallback()   {
           gapi.client.setApiKey('AIzaSyB7p7mH_vZGgtrbF4ntmKN2nBcsRyRFY1w');
           gapi.client.load('tictactoe2D', 'v1');   
       }
   </script>

   <script type="text/javascript" src="js/render.js"></script>
</head>

这是来自render.js的代码,添加了处理所有方块的点击事件的功能,并且还与我的端点方法进行通信-

$(document).ready(function() {
    $("#board td").click(function() {
        $(this).html("X");

        var boardString = [];
        var buttons = document.querySelectorAll('td');
        for (var i=0; i<buttons.length; i++)    {
            boardString.push(buttons[i].innerHTML);
        }

        boardString.join('');

        gapi.client.tictactoe2D.compute2DMove({'state': boardString}).execute(function(resp)    {
            document.write(resp.result.state);
        });
    });
});

这是整个代码的JSFiddle

当我尝试单击游戏板上的某个方块时出现问题。出乎意料,什么都没有发生。 我在 Chrome 中打开 JavaScript 控制台,发现每次点击方块时控制台都会显示以下错误-

       Uncaught TypeError: Cannot read property 'compute2DMove' of undefined

我无法弄清楚为什么会发生这种情况,以及如何解决它。有人可以帮我吗? 非常感谢您。

【问题讨论】:

    标签: javascript jquery google-app-engine google-cloud-endpoints


    【解决方案1】:

    在我在 JS 控制台上观察到一个新错误后,我自己修复了这个错误 - 错误是 -

    Failed to load resource: The server responded with a status of 403 (Forbidden)
    

    我检查了服务器返回的JSON对象,对象是这样的-

    {"error":{"errors":[{"domain":"global","reason":"sslRequired","message":"SSL is required to perform this operation."}],"code":403,"message":"SSL is required to perform this operation."}}
    

    看到这个后,我猜测没有使用安全连接(HTTPS)连接到应用程序,这就是服务器没有加载所需数据的原因。为了解决这个问题,我需要强制每次加载应用程序时都使用安全连接。 为此,我在 web.xml 中添加了以下标签-

    <security-constraint>
       <user-data-constraint>
          <transport-guarantee>CONFIDENTIAL</transport-guarantee>
       </user-data-constraint>
    </security-constraint>
    

    我在 apppot 上更新了我的应用程序,并检查了 JS 控制台。 那里没有错误,我的端点方法也执行了。我希望这些信息对遇到类似问题的人有所帮助。

    此外,我发现 gapi 库过去存在一些稳定性问题,保护 API 调用免受任何可能缺陷影响的最佳方法是使用 @ 构造 REST 请求987654326@,而不是发出 JSON-RPC 请求。(请参阅,Constructing REST requests using gapi.client.request)。构建 REST 请求可能既冗长又麻烦,但也很安全。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 2020-06-09
      • 1970-01-01
      相关资源
      最近更新 更多