Spring MVC, jQuery with ajax - getting 415

I am using jQuery to send an AJAX POST request. If successful, I need to redirect the user to another page, if I refuse, I want to stay on the page with the same data.

I have 2 questions.

  • In SUCCESS it is not redirected, it stays at the same URL and gives 415
  • In FAILURE, I get 415 instead of the page I get.

Here is my code:

 var jsonResponse = [];
 //jsonResponse is being constructed from the form data, on submit.

 var req = {
            "name" : "${name}",
            "id" : "${id}",
            "data" : jsonResponse
    };


 $.ajax({
        url: "url",
        type: "POST",
        dataType: 'json',
        contentType:'application/json',
        async: false,
        data: JSON.stringify(req),
        success: function(result) {

            url = window.location.href;
            url = url.replace("processData", "getData");
            alert(url); //prints localhost:8000/getData
            location.href = url; //stays on localhost:8000/processData
        },
        error: function() {
            alert("--- Failure ---");
            // should stay on same page with the previous data, but
            //returns a 415.
        }
    });

Controller for url1

 @RequestMapping(value = "processData", method = RequestMethod.POST)
public String post( Model model,
                    HttpServletRequest httpServletRequest, 
                    @RequestBody FormModel model) {

}

Controller for url2:

@RequestMapping(value = "getData", method = RequestMethod.GET)
public String get(Model model, HttpServletRequest httpServletRequest)
{
}

I am adding a snapshot of the error message that I am receiving. enter image description here

What am I doing wrong here?

+4
source share
1 answer

. "", AJAX. .

:

<input type="Submit" value="Submit" />

:

<input type="button" value="Submit" />

.

 $('form').on('submit', function(e) {
    e.preventDefault();

, : AJAX - GET POST

0

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


All Articles