1

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

}

4

3 回答 3

0

尝试这个:

var form = {
            "name": $("#name").val(),
            "description": $("#description").val(),
            "price": $("#price").val(),
            "accessible": $("#accessible").val()
        }
于 2013-06-30T23:27:26.573 回答
0

我认为问题出在

data: JSON.stringify(form)

您不必进行字符串化,只需简单地

data: form
于 2013-06-30T23:50:02.950 回答
0

问题是复选框的值。

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

于 2013-07-01T15:49:01.567 回答