【发布时间】: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