Ajax响应给出了undefined

时间:2014-10-07 10:31:48

标签: jquery ajax spring

我正在尝试添加CRUD操作的产品。首先我没有ajax这样做。现在我学习ajax并转换为ajax。当我添加产品时,它会返回(显示)" Product was created: [undefined:undefined:undefined]"。它不会打印值,也不会在数据库中更新。

位指示

@RequestMapping("addproduct.html")
    public String viewaddProduct(Map model) {
        Product product = new Product();   
        model.put("product", product);
        return "addproduct";
    }

    @RequestMapping(value ="addproduct.html", method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Product addProduct(@RequestBody Product product,
             Map model) {
            model.put("productList", productService.listProducts());
            model.put("categoryList", categoryService.listCategories());
           return     productService.addProduct(product);

ProductDAOImpl

public Product addProduct(Product product) {
        return (Product) sessionFactory.getCurrentSession().save(product);
    }

ProductServiceImpl

@Transactional
    public Product addProduct(Product product) {
        return productDAO.addProduct(product);
    }

JSP

<script>$(document).ready(function() {

      $('#Form').submit(function(event) {

          var productName = $('#productName').val();
          var price = $('#price').val();
          var quantity = $('#quantity').val();
          var json = { "productName" : productName, "price" : price, "quantity": quantity};

        $.ajax({
            url: $("#Form").attr( "action"),
            data: JSON.stringify(json),
            type: "POST",

            beforeSend: function(xhr) {
                xhr.setRequestHeader("Accept", "application/json");
                xhr.setRequestHeader("Content-Type", "application/json");
            },
            success: function(product) {
                var respContent = "";

                respContent += "<span class='success'>Product was created: [";
                respContent += product.productName + " : ";
                respContent += product.price + " : " ;
                respContent += product.quantity + "]</span>";

                $("#FromResponse").html(respContent);       
            }
        });

        event.preventDefault();
      });

    });</script>
<body>

<div id="FromResponse"></div> 
<h3> Form</h3>
<FONT color="blue"></FONT>
<form:form id="Form" action="addproduct.html" commandName="product" method="POST">
<table>
<tr><td>Product Name:<FONT color="red"><form:errors path="productName" /></FONT></td></tr>
<tr><td><form:input path="productName" /></td></tr>
<tr><td>Price:<FONT color="red"><form:errors path="price" /></FONT></td></tr>
<tr><td><form:input path="price" /></td></tr>
<tr><td>Quantity:<FONT color="red"><form:errors path="quantity" /></FONT></td></tr>
<tr><td><form:input path="quantity" /></td></tr>

<tr><td>Category:<FONT color="red"><form:errors path="category.cid" /></FONT></td></tr>
<tr><td><form:input path="category.cid" /></td></tr>
<tr><td>Description:<FONT color="red"><form:errors path="description" /></FONT></td></tr>
<tr><td><form:textarea path="description" cols="65" rows="10"/></td></tr>

<tr><td><input type="submit" value="Add Products" /></td></tr>
</table>
</form:form>

</body>

0 个答案:

没有答案