rocketmanu
rocketmanu

Reputation: 137

Jquery ajax get error event when code in controller run ok

i use jquery to send ajax request to spring mvc controller. Code in controller run ok but jquery get error event. When i try "inspect element" in chrome, i got message

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

My 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);
    }
});

My controller:

@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);
}

Thanks for your support.

Upvotes: 0

Views: 48

Answers (2)

rocketmanu
rocketmanu

Reputation: 137

I add @ResponseBody like below

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

and every thing works fine

Upvotes: 0

Jai
Jai

Reputation: 74738

Status 404

I doubt that you don't have a proper path of the method you are calling.

Issue seems to me that you are looking for the value which is not available in the request body:

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

As i can see you don't have anything named after this at the ajax side:

Neither here

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

nor here:

data : JSON.stringify(json),

Upvotes: 1

Related Questions