【发布时间】:2016-07-02 19:41:15
【问题描述】:
我对 angularJs 完全陌生(我实际上是一个纯粹的后端开发人员:()。 我观看了几个 youtube 视频,我正在尝试创建一个非常简单的 ui,它使用 angular 与后端的 Spring mvc 应用程序对话。
我遇到了一个问题,我不知道如何使用 $http.post 将 javascript 对象发送到我的后端。有人可以帮助我吗? 实际上,不仅如此,我在 html 上也有点废话,我不知道该怎么做 点击div时触发angularjs。
我向你展示我到目前为止在角度方面的知识......
这是我的看法
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TicTacToe</title>
<link rel="stylesheet" type="text/css" href="resources/css/design.css"/>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.js"></script>
<script src="resources/javascript/tictactoe.js"></script>
</head>
<body ng-controller="displayPageController">
<div id="player-1">PLAYER 1<br/>
{{Board.player1}}
</div>
<div id="game">
<div id="top-left" ng-click='playPage()'>{{Board.topleft}}</div>
<div id="top" ng-click='playPage()'>{{Board.top}}</div>
<div id="top-right" ng-click='playPage()'>{{Board.topright}}</div>
<div id="left" ng-click='playPage()'>{{Board.left}}</div>
<div id="middle" ng-click='playPage()'>{{Board.middle}}</div>
<div id="right" ng-click='playPage()'>{{Board.right}}</div>
<div id="bottom-left" ng-click='playPage()'>{{Board.bottomleft}}</div>
<div id="bottom" ng-click='playPage()'>{{Board.bottom}}</div>
<div id="bottom-right" ng-click='playPage()'>{{Board.bottomright}}</div>
</div>
<div id="player-2">PLAYER 2<br/>
{{Board.player2}}
</div>
</body>
</html>
这是我的 javascript
var myApp = angular.module('myApp',[]);
var gameBoardDto = {
player1:"0",
player2:"0",
topleft:".",
top:".",
topright:".",
left:".",
middle:".",
right:".",
bottomleft:".",
bottom:".",
bottomright:"."
};
myApp.controller('displayPageController', function($scope,$http) {
$scope.Board = {};
$scope.Board.player1 = gameBoardDto.player1;
$scope.Board.player2 = gameBoardDto.player2;
$scope.Board.topleft = gameBoardDto.topleft;
$scope.Board.top = gameBoardDto.top;
$scope.Board.topright = gameBoardDto.topright;
$scope.Board.left = gameBoardDto.left;
$scope.Board.middle = gameBoardDto.middle;
$scope.Board.right = gameBoardDto.right;
$scope.Board.bottomleft = gameBoardDto.bottomleft;
$scope.Board.bottom = gameBoardDto.bottom;
$scope.Board.bottomright = gameBoardDto.bottomright;
$scope.playPage = function() {
$http({
method: 'POST',
url: 'http://localhost:8080/play',
data: $scope.Board,
headers: {'Content-Type': 'application/json;charset=UTF-8'}
}).success(function(gameBoardDto) {
$scope.gameBoardDto = gameBoardDto;
}.bind(this));
};
});
更新 我想我设法使用上面的代码触发了该功能,现在我得到一个错误 400。我想现在我需要看看服务器端。
【问题讨论】:
-
那么具体的问题或疑问是什么?应该在服务器的请求正文中接收 json。如果不是...检查浏览器开发工具网络中的实际请求,并检查控制台是否有错误
-
@charlietfl 目前我在开发工具中没有错误。我刚刚更新了一张照片。我的问题是我不确定我是否做对了,这是正确的吗?我从未使用过角度,所以我不确定如何发送到后端。是的,我需要发送 JSON。网络选项卡没有显示太多,因为我认为单击 div 时我没有正确提交
标签: javascript angularjs post model-view-controller