要求找不到ajax spring mvc

时间:2016-06-07 07:47:32

标签: jquery ajax spring-mvc

我和人有一张桌子。当我点击删除图标时,我想从数据库中删除该人,并在成功时从表中删除该行。 我不断得到deleteEmployee?id=37 not found 404。 我有一个@RequestMapping(value = "/deleteEmployee", method = RequestMethod.GET)的控制器。 我点击的人已从数据库中删除(已选中),因此控制器应该没问题。但为什么我会收到错误?

@RequestMapping(value = "/deleteEmployee", method = RequestMethod.GET)
public void deleteEmployee(@RequestParam(value = "id", required = true) int id) {
    System.out.println(id);
    employeeDAO.deleteEmployee(id);
}


$(document).on('click','.delete-emp', function(){
    deleteEmployee(this);
});
function deleteEmployee(el){
    var id = $(el).parent().attr('data');
    console.log("delete: "+id);
    $.ajax({
        url: "deleteEmployee?id="+id,
        success: function(){
            deleteRow(id);
        }
    });
}

function deleteRow(el){
    var row = $('.employee-row[data='+el+']');
    var shiftRow = row.next();
    console.log("deleting "+row+" "+shiftRow);
    row.remove();
    shiftRow.remove();
}

1 个答案:

答案 0 :(得分:2)

在你的方法中使用@ResponseBody注释是足够的。这将解决您的问题。

@RequestMapping(value = "/android/api/home", method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void Home(Locale locale, Model model,HttpServletRequest request,HttpServletResponse response) throws IOException {
    //your logic
}

在ajax调用中添加内容类型

jQuery
        .ajax({
            url : controllerUrl,
            data : oMyForm,
            dataType : 'text',
            processData : false,
            contentType : false,
            type : 'POST',
            success : function(data) {

            }
        });