Solve the problem of returning the previous page and AJAX requests on the page, which is invalid for the Get request, and return the previous page ajax
First, let's analyze the cause of the problem.
Recently, when I was working on a project, I encountered a very common situation where the user entered the "my personal center, you can click the button to jump to the certification materials filling page. After clicking this button, you will be redirected to the certification materials filling page, fill in the materials, and submit the materials successfully. When the user directly returns to the previous page. You must change the authentication status to "authentication in progress ". In this case, you need to use an AJAX method to query the authentication status and modify the status display of the page.
At that time, the ajax method was not written according to the standard method. The default method is Get request. The front-end JS Code is as follows:
Window. onload = function () {var isProfesser = $ ('# isProfesser '). val (); var isreview =$ ('# isreview '). val (); var userid = $ ('# myid '). val (); if (isProfesser = '0' & isreview = '0') {$. ajax ({url: "/isAuthenticing", data: {userid: userid}, success: function (data, textStatus, jqXHR) {if (data. result = '1') {$ ('# approveadd a'0000.html ("+ authentication in progress") }}, error: function (){}});} // getMyQusetionInfo ();}
The backend controller receives GET by default.
@ RequestMapping (value = "/isAuthenticing", method = RequestMethod. GET) @ ResponseBodypublic Map <String, Object> isAuthenticing (@ RequestParam ("userid") String userid, HttpServletRequest request) throws IOException {// method body ;}
Solution
Practice has proved that the Get method is not feasible. Every time you move back to the previous page, the ajax method on the page will be executed as usual, but it will not request the controller in the background, the value returned by the request is also the value of the last page load. I am not clear about the specific reason.
Later, with a try, I used the POST request. unexpectedly, the POST request was used, and the process went through.
The front-end JS Code is as follows:
Window. onload = function () {var isProfesser = $ ('# isProfesser '). val (); var isreview =$ ('# isreview '). val (); var userid = $ ('# myid '). val (); if (isProfesser = '0' & isreview = '0') {$. ajax ({url: "/isAuthenticing", data: {userid: userid}, type: 'post', success: function (data, textStatus, jqXHR) {if (data. result = '1') {$ ('# approveadd a'0000.html ("+ authentication in progress") }}, error: function (){}});} // getMyQusetionInfo ();}
Background code:
@ RequestMapping (value = "/isAuthenticing", method = RequestMethod. POST) @ ResponseBodypublic Map <String, Object> isAuthenticing (@ RequestParam ("userid") String userid, HttpServletRequest request) throws IOException {// method body}
Hope to help those who encounter the same problems as me.
Note: The red part is the modified part.
The above section describes how to solve the problem of returning AJAX requests on the previous page and page, which is invalid for Get requests. I hope this will help you, if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!