Javascript 使用Ajax-springmvc发送表单
我在使用Ajax发送表单时遇到问题 以下是表格:Javascript 使用Ajax-springmvc发送表单,javascript,jquery,ajax,spring,spring-mvc,Javascript,Jquery,Ajax,Spring,Spring Mvc,我在使用Ajax发送表单时遇到问题 以下是表格: <form method="POST" id="add-card-form" action="${pageContext.request.contextPath}/card/add" class="form-horizontal"> <select name="type" class="form-control"> <c:forEach items="${cardTypes}" var="cardType"
<form method="POST" id="add-card-form" action="${pageContext.request.contextPath}/card/add" class="form-horizontal">
<select name="type" class="form-control">
<c:forEach items="${cardTypes}" var="cardType">
<option value="${cardType.id}">${cardType.name}</option>
</c:forEach>
</select>
<select name="category" class="form-control">
<c:forEach items="${cardCategories}" var="cardCategory">
<option value="${cardCategory.id}">${cardCategory.name}</option>
</c:forEach>
</select>
<textarea type="text" name="description" class="form-control" rows="6"></textarea>
<input type="submit" id="add-card-submit" value="Add card" class="btn btn-primary"/>
这是一个控制器动作:
@RequestMapping(value="/add", method = RequestMethod.POST)
public @ResponseBody Card addCard(@RequestBody Integer type,
@RequestBody Integer category,
@RequestBody String description) {
Card card = new Card();
card.setType(cardTypeService.findById(type));
card.setCategory(cardCategoryService.findById(category));
card.setDescription(description);
card.setOwner(1);
cardService.saveCard(card);
System.out.println("Card with id " + card.getId() + " added!");
return card;
}
可变数据值:
Object {type: 1, category: 1, description: "New Card"}
当我尝试发送此表单时,总是出现错误400:http://localhost:8080/card/add 400(错误请求)
你能告诉我这个代码有什么问题吗?我写过一些关于使用SpringMVC+Ajax发送数据的帖子和文章,但没有人帮忙
编辑:
我将@RequestBody更改为三个@RequestParams:
@RequestMapping(value="/add", method = RequestMethod.POST)
public @ResponseBody Card addCard(@RequestParam("type") Integer type,
@RequestParam("description") String description,
@RequestParam("category") Integer category) {
我仍然得到400个错误。以下是原始HTTP请求:
POST/card/add HTTP/1.1
主机:本地主机:8080
连接:保持活力
内容长度:48
缓存控制:没有缓存
Pragma:没有缓存
来源:http://localhost:8080
X-request-With:XMLHttpRequest
内容类型:application/json;字符集=UTF-8
接受:application/json,text/javascript,*/*;q=0.01
用户代理:Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/36.0.1985.125 Safari/537.36
推荐人:http://localhost:8080/
接受编码:gzip、deflate、sdch
接受语言:pl,pl;q=0.8,在美国;q=0.6,en;q=0.4
Cookie:JSESSIONID=ABAD895C419A9175EAB4D0833C543724
和数据对象:
category: 1
description: "New Card"
type: 1
@RequestBody
表示HTTP请求正文的内容将映射到属性。由于您只能有一个请求主体,这几乎肯定会失败
您很可能需要指定请求参数:@RequestParam(“type”)字符串类型
如果这没有帮助,您能否从web服务器的日志/控制台提供原始HTTP请求数据和Spring MVC错误堆栈跟踪?
@RequestBody
表示HTTP请求正文的内容将映射到属性。由于您只能有一个请求主体,这几乎肯定会失败
您很可能需要指定请求参数:@RequestParam(“type”)字符串类型
如果这没有帮助,您能否从web服务器的日志/控制台提供原始HTTP请求数据和Spring MVC错误堆栈跟踪?Dmitry Zolotukhin说得对,唯一合理的做法是只有一个
@RequestBody
,因为您只能使用一次请求,在此之后,它将被提交,并且所有后续的读取尝试都将失败
您应该做的是让您的卡
类作为方法参数,用@RequestBody
注释,所以
@RequestMapping(value="/add", method = RequestMethod.POST)
public @ResponseBody Card addCard(@RequestBody Card card) {
card.setOwner(1);
cardService.saveCard(card);
System.out.println("Card with id " + card.getId() + " added!");
return card;
}
springmvc框架将负责Card
对象的实例化,并通过将JSON
主体中的键与属性匹配,将请求值绑定到属性。还要注意,要使其工作,您发送的数据必须是有效的json,因此请确保符合该要求
可以考虑创建一个简单的数据传输对象,例如<代码> CardDTO < /> >像
之类的东西。public class CardDTO {
private Integer category;
private Integer type;
private String descrption;
public Integer getCategory() {
return category;
}
public void setCategory(Integer category) {
this.category = category;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getDescrption() {
return descrption;
}
public void setDescrption(String descrption) {
this.descrption = descrption;
}
}
而不是拘束于它
@RequestMapping(value="/add", method = RequestMethod.POST)
public @ResponseBody Card addCard(@RequestBody CardDTO cardDTO) {
Dmitry Zolotukhin说得对,唯一合理的做法是只有一个
@RequestBody
,因为您只能使用一次请求,之后请求将被提交,所有后续读取尝试都将失败
您应该做的是让您的卡
类作为方法参数,用@RequestBody
注释,所以
@RequestMapping(value="/add", method = RequestMethod.POST)
public @ResponseBody Card addCard(@RequestBody Card card) {
card.setOwner(1);
cardService.saveCard(card);
System.out.println("Card with id " + card.getId() + " added!");
return card;
}
springmvc框架将负责Card
对象的实例化,并通过将JSON
主体中的键与属性匹配,将请求值绑定到属性。还要注意,要使其工作,您发送的数据必须是有效的json,因此请确保符合该要求
可以考虑创建一个简单的数据传输对象,例如<代码> CardDTO < /> >像
之类的东西。public class CardDTO {
private Integer category;
private Integer type;
private String descrption;
public Integer getCategory() {
return category;
}
public void setCategory(Integer category) {
this.category = category;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getDescrption() {
return descrption;
}
public void setDescrption(String descrption) {
this.descrption = descrption;
}
}
而不是拘束于它
@RequestMapping(value="/add", method = RequestMethod.POST)
public @ResponseBody Card addCard(@RequestBody CardDTO cardDTO) {
我无法将请求参数绑定到卡中,因为我的卡具有CardCategory和CardType等属性,而不是简单的整数。这就是为什么我在保存之前使用findById作为type和category。有没有办法绑定只有id号的CardCategory和CardType?最简单的方法是创建一个专用的数据传输类,只绑定属性,我编辑了一个答案谢谢,最简单的解决方案通常是最难找到的:)一切都很完美:)我无法将请求参数绑定到卡中,因为我的卡具有CardCategory和CardType等属性,而不是简单的整数。这就是为什么我在保存之前使用findById作为type和category。有没有办法绑定只有id号的CardCategory和CardType?最简单的方法是创建一个专用的数据传输类,只绑定属性,我编辑了一个答案谢谢,最简单的解决方案往往最难找到:)一切都很好:)