This article introduces WeChat to return to the previous page, the AJAX request in the page, the analysis and solution of the problem of invalid Get request, friends in need can refer to it
Let me first analyze the cause of the problem
When I was working on a WeChat project recently, I encountered a very common situation. The demand is like this. When the user enters "My Personal Center", there will be a button to click to jump to fill in the certification information. After clicking this button, it will jump to the certification information filling page, fill in the information, and submit it 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(); }
Background controller The default received is also GET
@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 It doesn't work. Every time you go back to the previous page, the ajax method in the page will be executed as usual, but it will not request the background controller. The value returned by the request is also the value when the page was last loaded. I don't know the specific reason for the time being. clear.
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(); }
The back-end code:
@RequestMapping(value = "/isAuthenticing", method = RequestMethod.POST) @ResponseBody public Map<String, Object> isAuthenticing(@RequestParam("userid") String userid,HttpServletRequest request) throws IOException { //方法体 }
Hope it can help those who encounter the same problem as me.
Note: The red part is the modified part
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
ajax uses json to realize data transmission
Ajax transfers JSON example code
The post method in Ajax directly returns a number starting with 0 and error analysis
##
The above is the detailed content of Solve the problem that WeChat returns to the previous page and the AJAX request in the page is invalid for the Get request. For more information, please follow other related articles on the PHP Chinese website!