gpt4 book ai didi

java - Spring ajax 400(错误请求)

转载 作者:行者123 更新时间:2023-12-01 04:35:43 25 4
gpt4 key购买 nike

我尝试开发简单的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!");
}
})

})
})

Controller :

   @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;
}

}

最佳答案

试试这个:

var form = {
"name": $("#name").val(),
"description": $("#description").val(),
"price": $("#price").val(),
"accessible": $("#accessible").val()
}

关于java - Spring ajax 400(错误请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17395437/

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