繁体   English   中英

Spring Ajax 400(错误请求)

[英]Spring ajax 400 (Bad Request)

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

}

尝试这个:

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

我认为问题出在

data: JSON.stringify(form)

您不必简单地进行分类

data: form

问题是checkBox的值。

当我调用$("#accessible").val() on返回字符串。 应该是true还是false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM