gpt4 book ai didi

javascript - 我无法将数据从 AngularJS 发送到 Spring MVC Controller

转载 作者:行者123 更新时间:2023-12-03 01:51:49 25 4
gpt4 key购买 nike

如何将表单值传递到 Angular 脚本,以及从脚本传递到 Spring MVC Controller 。下面我编写了表单数据无法发送 Controller 的代码

脚本

<script type="text/javascript">
var app = angular.module('formSubmit', []);

app.controller('FormSubmitController',[ '$scope', '$http', function($scope, $http) {

$scope.list = [];
$scope.headerText = 'AngularJS Post Form Spring MVC example: Submit below form';
$scope.formData = {};
$scope.submit = function() {

var formData = {
"name" : $scope.name,
"salary" : $scope.salary,
"designation" : $scope.designation,
};

var response = $http.post('submitmock',{ Emp: formData}); //passing Emp
response.success(function(data, status, headers, config) {
$scope.list.push(data);
});


response.error(function(data, status, headers, config) {
alert("Exception details: " + JSON.stringify({
data: $scope.formData //used formData model here
}));
});

//Empty list data after process
$scope.list = [];

};
}]);

Spring MVC Controller

  @RequestMapping(value = "submitmock", method = RequestMethod.POST)
public @ResponseBody Emp getMock(@RequestBody Emp Emp)
{
serviceInD.savedata(Emp);
return Emp;
}

最佳答案

也许您需要在 Spring MVC 中允许跨源

像这样:

@CrossOrigin
@RequestMapping(value = "submitmock", method = RequestMethod.POST)
public @ResponseBody Emp getMock(@RequestBody Emp Emp)
{
serviceInD.savedata(Emp);
return Emp;
}

您可以引用 spring 指南: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-cors-controller

关于javascript - 我无法将数据从 AngularJS 发送到 Spring MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50387748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com