您好我正在用Angularjs創建一個應用程序,REST服務與春天。我想從angulajs網址休息服務傳遞對象,但它的工作,請任何一個可以幫助我,我的jsp頁面的代碼如下圖所示,將對象從angularjs控制器傳遞到休息API
<html ng-app="studentApp">
<body>
<div ng-controller="studentController">
<table border = "0">
<tr>
<td>Enter first name:</td>
<td><input type = "text" ng-model = "student.firstName"></td>
</tr>
<tr>
<td>Enter last name: </td>
<td>
<input type = "text" ng-model = "student.lastName">
</td>
</tr>
</table>
</div>
</body>
</html>
和我angularjs代碼,
var studentApp = angular.module("studentApp", []);
studentApp.config(['$httpProvider', function ($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);
studentApp.controller("studentController", [ '$scope', '$http',
function($scope, $http) {
$scope.toggle=true;
var urlBase="http://localhost:8080/studentweb/";
$scope.insertStudent = function inserStudent() {
$http({
method : 'POST',
url : urlBase+'/Student/insert',
data: {student:data}
}).success(function(data, status, headers, config) {
$scope.student=data;
$scope.toggle='!toggle';
}).error(function(data, status, headers, config) {
alert("failure1");
});
和我的休息服務,
public class StudentController
{
@RequestMapping(value="/Student/insert",method = RequestMethod.POST ,params= {"student"})
public String insertStudent(@RequestParam("student") StudentVO student) throws ParseException {
student.setFirstName(student.getFristName());
student.setLastName(student.getLstName());
studentcontrol.addStudent(student);
return "";
}
}
} ])
我猜$ .params是jQuery。我還沒有在文檔中找到任何這樣的功能。另外,在發佈的HTML中沒有ng-app。發佈一個重現問題的最小完整示例,並告訴*如何*它不起作用。打開瀏覽器控制檯並檢查錯誤消息。爲什麼你的REST API不接受JSON?這會讓事情變得更容易。 –
警報消息failure1即將到來。控制檯上沒有錯誤日誌 – premashree
您現在正在將請求正文中的JSON對象(作爲JSON)發送到不期待JSON的REST服務。 –