Solve the problem that WeChat returns the previous page, AJAX requests in the page, and the Get request is invalid.

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.