我正在尝试使用 spring、angularjs 和 hibernet 将数据插入数据库。
在app.js文件中 Controller 方法
$http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City)
不起作用我不知道我犯了什么错误。
AngularDate.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html ng-app="taskManagerApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>AngularJS Task Manager</title>
<script data-require="angular.js@*" data-semver="1.2.13" src="http://code.angularjs.org/1.2.13/angular.js"></script>
<script src="<c:url value=" /resources/js/app.js "/>"></script>
</head>
<body>
<div ng-controller="taskManagerController">
<span>Add Task</span>
<div>
<div>
<table>
<tr>
<td> Name:</td>
<td>
<input type="text" ng-model="Name" />
</td>
</tr>
<tr>
<td>City:</td>
<td>
<input type="text" ng-model="City" />
</td>
</tr>
<tr>
<td>
<button ng-click="addTask()" class="btn-panel-big">Add New Task</button>
</td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
app.js
var taskManagerModule = angular.module('taskManagerApp', []);
var t = angular.module('taskManagerController', []);
taskManagerModule.controller('taskManagerController', function ($scope, $http) {
var urlBase = "http://localhost:8081/logistics";
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
//get all tasks and display initially
//add a new task
$scope.addTask = function addTask() {
if ($scope.Name == "" || $scope.City == "") {
alert("Insufficient Data! Please provide values for name and city");
}
else {
alert("else");
$http.post(urlBase + '/angular/insert/' + $scope.Name + '/' + $scope.City).success(function (data) {
alert("Task added");
// $scope.tasks = data;
$scope.Name = "";
$scope.City = "";
//$scope.toggle='!toggle';
});
}
};
// Archive Completed Tasks
});
//Angularjs Directive for confirm dialog box
有人可以帮我解决我的问题吗?
最佳答案
$http
帖子需要 POST 正文,因此如果您有它,请替换它
$http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City)
与
//JSON data to post
var postData = {
...
...
}
$http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City, postData).success(
function(data) {
//success code
})
或者如果您没有任何发布数据,则只需发送一个空的 JSON 对象,例如
$http.post(urlBase + '/angular/insert/' +$scope.Name+'/'+$scope.City,{}).success(
function(data) {
//success code
})
关于javascript - $http.post() 方法在 Angular js 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44984866/