作者:好人好报 | 来源:互联网 | 2023-09-14 17:37
篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何将数据从angularjs传递给spring控制器相关的知识,希望对你有一定的参考价值。
Angular js代码
var app = angular.module('myApp', ['ngResource']); app.controller('UserController', ['$scope', '$resource', '$http',function($scope,$resource,$http) { $scope.deleteRec = function() { $http({ method: 'DELETE', url: 'delete/:username', data: {username: $scope.myform.username}, headers: {'Content-type': 'application/json;charset=utf-8'} }).then(function(response){ $scope.Messages = response; }); }; }]); user_name phone email delete delete
控制器代码
@RequestMapping(value="/delete/{username}") @ResponseBody public String delete(@PathVariable String username) { System.out.println("delete1"); String user=retrievedataservice.delete(username); System.out.println("delete2"); return "delete successfully"; }
实际上,它的用户名不能从角度js传递给spring Controller。它的错误就像请求处理失败一样;嵌套异常是java.lang.NullPointerException说明服务器遇到意外情况,导致服务器无法完成请求。
答案
您应该将用户名传递给deleteRec()
方法。因为你正在使用ng-bind
,它是单向数据绑定。
和
$scope.deleteRec = function(username) { $http({ method: 'DELETE', url: 'delete/:username', data: {username: username}, headers: {'Content-type': 'application/json;charset=utf-8'} }).then(function(response){ $scope.Messages = response; }); }; }]);