JQuery Ajax request with error 404 (not found), but it works

I am sending a JQuery AJAX request to the server, but the browser console tells me that the page was not found. However, my Spring MVC signature matching the requested URL is running, and yet donepart of the AJAX function is missing.

Here is the relevant code:

JavaScript:

    var content = $(data).filter("#wrapper-email-content");
    $.ajax({
        url : '/sendEmail',
        type : 'POST',
        data : {
            content: content.html()
        }
    }).done(function(){
        console.log("Finished")
    });

Spring Signature MVC:

@RequestMapping(value = "/sendEmail", method = RequestMethod.POST)
public void sendEmail(
    HttpServletRequest request, String content) {

    UserTO user = (UserTO) request.getSession().getAttribute("USER");
    String email = user.getEmail();
    String title = "Your order";
    Email.sendEmail(title, email, content, null, null, null);
}

So, the email is sent in the code, which means that the sendEmail method is executed, but I still get Error 404: not found from the AJAX request, and Finish is not printed on the console.

What am I doing wrong?

+4
source share
3 answers

-

@ResponseStatus(value = HttpStatus.OK)

. fooobar.com/questions/61616/... .

+1
@RequestMapping(value = "/sendEmail",headers="Accept=application/json", method = RequestMethod.POST)
public Map<String,String> sendEmail(
    HttpServletRequest request, @RequestBody String content) {
    Map<String,String> statusMap=new HashMap<String,String>();
    UserTO user = (UserTO) request.getSession().getAttribute("USER");
    String email = user.getEmail();
    String title = "Your order";
    Email.sendEmail(title, email, content, null, null, null);
    statusMap.put("status","Mail sent successfully");
    return statusMap;
}
+2

EDIT: see solution posted for What to return if Spring MVC controller method does not return a value?

Not familiar with Spring MVC, but you probably need to return the success status code yourself:

public String sendEmail(
    HttpServletRequest request, @RequestBody String content) {

    UserTO user = (UserTO) request.getSession().getAttribute("USER");
    String email = user.getEmail();
    String title = "Your order";
    Email.sendEmail(title, email, content, null, null, null);
    return new ResponseEntity<String>(json,HttpStatus.OK);
}
+1
source

Source: https://habr.com/ru/post/1620111/


All Articles