当控制器中的代码运行正常时,Jquery ajax会收到错误事件

时间:2015-10-29 08:44:48

标签: jquery ajax spring

我使用jquery向spring mvc控制器发送ajax请求。 控制器中的代码运行正常但是jquery获取错误事件。 当我尝试"检查元素"在chrome中,我收到了消息

Failed to load resource: the server responded with a status of 404 (Introuvable)

我的jquery:

var json = {
    'projectsCode' : projectCodeFromTargetTable,
    'start' : start,
    'end' : end,
    'repeatType' : repeatType,
    'runNow' : runNow
}

$.ajax({
    url : "auto-email-config/schedule",
    type : 'POST',
    contentType: 'application/json',
    data : JSON.stringify(json),
    success : function(response) {
        alert('sucess sending json');
    },
    complete : function() {
        $("#setupSchedule").prop("disabled", true);
    },
    error : function(jqXhr, textStatus, errorThrown) {
        alert(textStatus);
    }
});

我的控制器:

@RequestMapping(value = "/schedule", method = RequestMethod.POST)
public void scheduleAutoEmail(@RequestBody String scheduleInfo, ModelMap model) throws IOException,
        SchedulerException {

    LOGGER.info("Enter schedule auto email " + scheduleInfo);

    ObjectMapper mapper = new ObjectMapper();
    // map json object from client to AutoEmailMapper class
    AutoEmailMapper emailMapper = mapper.readValue(scheduleInfo, AutoEmailMapper.class);

    emailService.scheduleEmail(emailMapper);
}

感谢您的支持。

2 个答案:

答案 0 :(得分:1)

状态404

我怀疑你没有正确的方法来调用你的方法。

在我看来,您正在寻找请求正文中没有的值:

public void scheduleAutoEmail(@RequestBody String scheduleInfo, ModelMap model)
//-----------------------------------------^^^^^^^^^^^^^^^^^^^---this one

我可以看到你在ajax方面没有任何名字:

不在这里

var json = {
        'projectsCode' : projectCodeFromTargetTable,
        'start' : start,
        'end' : end,
        'repeatType' : repeatType,
        'runNow' : runNow
    }

也不在这里:

data : JSON.stringify(json),

答案 1 :(得分:0)

我在下面添加@ResponseBody

@RequestMapping(value = "/schedule", method = RequestMethod.POST)
public @ResponseBody void scheduleAutoEmail(...){
   ...
   return;
}

并且一切正常