在Spring mvc中使用jQuery Ajax但不能调用控制器函数

时间:2015-11-30 13:38:12

标签: jquery ajax spring spring-mvc

我尝试使用jQuery Ajax来部分更新html内容。我查看了ajax如何与spring mvc合作,我遵循了一些教程。 但我无法得到理想的结果。似乎控制器甚至没有被调用。 如何解决这个bug? 提前谢谢。

jsp文件

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>管理员面板</title>
<spring:url value="/resources/css/minputs.css" var = "minputscss"/>
<link  rel="stylesheet" href = "${minputscss}" type ="text/css"/>
<script src="<c:url value='/resources/js/button.js'/>" type="text/javascript" />
<script src="<c:url value='/resources/js/jquery-1.6.4.min.js' />" type="text/javascript"/>
<script src="<c:url value ='/resources/js/commen.js'/>" type="text/javascript"/>
</head>
<body style="background:#202020">
<div id="mainContent" style="width:80%;float:right">

<table>
<tr>title:<input id="title"/></tr>
<tr>content:<input id="content"/><tr>   
<tr>
<button type="button" id="submitButton" onclick="submitButton();" value="submit">submit</button>
</tr>
</table>
</div>
</body>
</html>
<script>

function submitButton() {
    var title = document.getElementById("title").value;
    var content = document.getElementById("content").value;
    alert("test"+title+content);

    $.ajax({
        type : "POST",
        contentType : "application/json",
        url : "submitArticle",
        data :{
            title:title,
            content:content
            },
        dataType : 'json',
        timeout : 100000,
        success : function(data) {
            console.log("SUCCESS: ", data);
            alert(data);
        },
        error : function(e) {
            console.log("ERROR: ", e);
            alert(e);
        },
        done : function(e) {
            console.log("DONE");
            alert("done");
        }
    });
}



</script>

控制器

package com.plainart.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.plainart.entity.Article;
import com.plainart.service.UploadService;

@Controller
public class EditorController {


    @Autowired
    private UploadService uploadService;

    @RequestMapping(value ="/submitArticle", method =RequestMethod.POST)
    public @ResponseBody
    String submitArticle(@RequestBody Article art,HttpServletRequest request){
        Date now = new Date();
        System.out.println("receive");
        uploadService.uploadArticleInfo(art);
        String path = uploadService.uploadArticle(art.getTitle()+art.getId(), art.getContent());
        return path;
    }

}

1 个答案:

答案 0 :(得分:0)

尝试更改dataType

dataType : 'text',  

因为你没有从方法中产生任何json。

当您使用@RequestBody注释时,必须发送请求:

 data :{
    art: "someArt"
    title:title,
    content:content
 },