Home > Web Front-end > JS Tutorial > Solve the problem that WeChat returns to the previous page and the AJAX request in the page is invalid for the Get request

Solve the problem that WeChat returns to the previous page and the AJAX request in the page is invalid for the Get request

亚连
Release: 2018-05-23 10:07:04
Original
1722 people have browsed it

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();
}
Copy after login

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 {
//方法体;
}
Copy after login

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=$(&#39;#isProfesser&#39;).val();
  var isreview=$(&#39;#isreview&#39;).val();
  var userid=$(&#39;#myId&#39;).val();
  if(isProfesser==&#39;0&#39; && isreview==&#39;0&#39;){
   $.ajax({
     url:"/isAuthenticing",
     data: {userid:userid},     type: &#39;POST&#39;,     success: function (data, textStatus, jqXHR) {
      if(data.result==&#39;1&#39;){
        $(&#39;#approveadd a&#39;).html("+认证中")
      }
     },
     error: function () {
     }
   });
  }
  //getMyQusetionInfo();
}
Copy after login

The back-end code:

@RequestMapping(value = "/isAuthenticing", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> isAuthenticing(@RequestParam("userid") String userid,HttpServletRequest request) throws IOException {
//方法体
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template