This time I will bring you the AJAX Get request that is invalid in WeChat Return to the previous page , solve the problem that the AJAX Get request is invalid in WeChat return to the previous page Notes What are they? Here are actual cases. Let’s take a look.
Let me first analyze the cause of the problem
Recently when I was working on a WeChat project, I encountered a very common situation. The requirement It's like this. When the user enters "My Personal Center", there will be a button that clicks to jump to fill in the certification information. After clicking this button, it will jump to the page for filling in the certification information. Fill in the After the information is submitted successfully. When the user returns directly to the previous page. The authentication status needs to be changed to "Authenticating". At this time, you need to use an AJAX method to query the authentication status and modify the status display on the page.
At that time, the ajax method was not written according to the standard method. The default method was 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').html("+认证中") } }, error: function () { } }); } //getMyQusetionInfo(); }
The background controller also receives GET by default
@RequestMapping(value = "/isAuthenticing", method = RequestMethod.GET) @ResponseBody public Map<String, Object> isAuthenticing(@RequestParam("userid") String userid,HttpServletRequest request) throws IOException { //方法体; }
Solution
Practice has proved that using the Get method is not feasible. Every time you return to the previous page, the ajax method in the page will be executed as usual, but It will not request the background controller, and the value returned by the request is also the value when the page was last loaded. The specific reason is not clear to me at the moment.
Later, with the mentality of giving it a try, I used POST request. Surprisingly, using POST request, the whole process went smoothly.
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').html("+认证中") } }, error: function () { } }); } //getMyQusetionInfo(); }
Back-end code:
@RequestMapping(value = "/isAuthenticing", method = RequestMethod.POST) @ResponseBody public Map<String, Object> isAuthenticing(@RequestParam("userid") String userid,HttpServletRequest request) throws IOException { //方法体 }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website !
Recommended reading:
Asynchronous file or image upload ajax
AJAX interacts with the database to determine whether the user is registered
The above is the detailed content of AJAX Get request is invalid when returning to the previous page in WeChat. For more information, please follow other related articles on the PHP Chinese website!