我正在尝试发出AJAX帖子请求,但是我的JSON对象值没有映射到我的控制器中的Java对象。当我调试Java对象字段时,我得到返回的null值。请参阅下面的代码。
AJAX请求
$('#form').submit(function(e) {
e.preventDefault();
var account = {};
account.type = $('#account-type option:selected').text();
account.name = $('#account-names option:selected').text();
account.amount = $(this).find('input[name=amount]').val();
$.ajax({
contentType: 'application/json',
url: '/spring-mvc-practice/account/create',
type: 'POST',
dataType: 'json',
data: JSON.stringify(account),
success: function(response) {
console.log("success");
},
error: function() {
console.log("error");
}
});
});
AccountController.java
@Controller
public class AccountController {
@RequestMapping(value = "/account/create", method = RequestMethod.POST, headers = {"Content-type=application/json"})
@ResponseBody
public String createAccount(@ModelAttribute Account account) {
System.out.println("name = " + account.getName());
System.out.println("type = " + account.getType());
System.out.println("amount = " + account.getAmount());
return null;
}
}
Account.java
public class Account {
private String name;
private String type;
private double amount;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
调试结果:
name = null
type = null
金额= 0.0
我还尝试在控制器方法中将@ModelAttribute
更改为@RequestBody
(根据本教程:https://gerrydevstory.com/2013/08/14/posting-json-to-spring-mvc-controller/),但在发出AJAX请求时出现此错误:
POST http://localhost:8080/spring-mvc-practice/account/create 415 (Unsupported Media Type)
任何帮助将不胜感激。感谢。
答案 0 :(得分:1)
如果您想使用createAccount
,只需将@ResponseBody
@RequestMapping(value = "/account/create", method = RequestMethod.POST)
public String createAccount(@RequestBody final Account account) {
System.out.println("name = " + account.getName());
System.out.println("type = " + account.getType());
System.out.println("amount = " + account.getAmount());
return null;
}
方法替换为:
$.ajax({
contentType: 'application/json;charset=UTF-8',
url: '/spring-mvc-practice/account/create',
dataType: 'json',
type: 'POST',
cache: false, // Force requested pages not to be cached by the browser
processData: false, // Avoid making query string instead of JSON
data: JSON.stringify(account)
}).done(function (data) {
console.log('AJAX call was successfully executed ;)');
console.log('data = ', data);
}).fail(function () {
console.log('AJAX call failed :(');
});
...你的AJAX调用应该有:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
...而且最重要的是,你必须在你的类路径中有这种依赖(你可以使用版本,以防你特别没有那个版本...注意我正在使用Maven):
{{1}}
答案 1 :(得分:-1)
请尝试将您的参赛者帐户指定为传递此类内容。从现在开始,您将它们作为数组传递,主要用于传递列表。例如,您想要列出帐户,然后您将使用上面的语法。
但是现在请试试这个:
var account = {type: $('#account-type option:selected').text(),name: $('#account-names option:selected').text(),amount : $(this).find('input[name=amount]').val()};