I am developing a simple Tic Tac Toe on Google App engine using cloud endpoints. My API name is tictactoe2D. I have only 1 endpoint method by the name tictactoe2D.compute2DMove() in my API which I have tested in the APIs explorer, and is working absolutely fine.
Now I am working to create the front end of the game, and using Google APIs JavaScript Client library to communicate with my endpoint method. I have followed exactly the same procedure as shown in the Getting Started page, which is a complete tutorial on using the library.
Here is code snippet from board.html, which loads the Javascript library-
<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>
And here is the code from render.js, which add the functionality to deal with click event of all squares, and also communicates with my endpoint method-
$(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);
});
});
});
Here is the JSFiddle for the whole code.
Problem occurs when I try to click one of the squares on the game board. Unexpectedly, nothing happens. I opened up the JavaScript console in Chrome, and found out that the console showed the following error whenever I click on a square-
Uncaught TypeError: Cannot read property 'compute2DMove' of undefined
I have not been able to figure out why is this happening, and how can I fix it. Can anybody please help me out? Thankyou very much in advance.