我尝试开发简单的ajax应用程序。但是当我发送数据时,我会在Chrome控制台中看到。
Clicked! create.js:12
Object {name: "df", description: "fd", price: "2", accessible: "on"} create.js:13
ERROR: POST http://localhost:8080/cqrsSpring/create 400 (Bad Request)
网页:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/create.js"></script>
<title>Create Product</title>
</head>
<body>
<label>Name</label>
<input id="name">
<label>Price</label>
<input id="price" type="number">
<label>Description</label>
<input id="description">
<label>Accesible</label>
<input id="accessible" type="checkbox">
<button id="send">Send</button>
</body>
</html>
脚本
$(document).ready(function () {
$("#send").click(function () {
var form = {
name: $("#name").val(),
description: $("#description").val(),
price: $("#price").val(),
accessible: $("#accessible").val()
}
console.info("Clicked!");
console.info(form);
$.ajax({
url: "create",
type: "POST",
dataType: "json",
contentType: "application/json",
data: JSON.stringify(form),
success: function (d) {
console.info("Success!");
}
})
})
})
控制器:
@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = "application/json")
public ModelAndView recCreate(@RequestBody CreateProduct product, BindingResult result) {
if (result.hasErrors())
return new ModelAndView("createProduct");
logger.info("Command recived " + product.toString());
bus.post(product);
return new ModelAndView("hello", "message", "Product " + product.getName() + " received!");
}
命令对象
public final class CreateProduct {
@NotNull
@NotBlank
private String name;
@NotBlank
@NotNull
private String description;
@Min(0)
private int price;
private boolean accessible;
public CreateProduct(int price, String name, boolean accessible, String description) {
this.price = price;
this.name = name;
this.accessible = accessible;
this.description = description;
}
@Override
public String toString() {
return "CreateProduct{" +
"price=" + price +
", name='" + name + '\'' +
", accessible=" + accessible +
", description='" + description + '\'' +
'}';
}
public int getPrice() {
return price;
}
public String getName() {
return name;
}
public boolean isAccessible() {
return accessible;
}
public String getDescription() {
return description;
}
//set
public void setPrice(int price) {
this.price = price;
}
public void setName(String name) {
this.name = name;
}
public void setAccessible(boolean accessible) {
this.accessible = accessible;
}
public void setDescription(String description) {
this.description = description;
}
}
答案 0 :(得分:0)
试试这个:
var form = {
"name": $("#name").val(),
"description": $("#description").val(),
"price": $("#price").val(),
"accessible": $("#accessible").val()
}
答案 1 :(得分:0)
我认为问题在于
data: JSON.stringify(form)
你没有字符串化,只是简单地
data: form
答案 2 :(得分:0)
问题是checkBox的价值。
当我致电$("#accessible").val()
时,会返回字符串on
。应为true
或false
。