AJAX request, solving the problem of invalid Get request

小云云
Release: 2023-03-19 11:44:01
Original
2717 people have browsed it

This article mainly 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, I hope it can help everyone.

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

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

Solution

Practice has proved that using the Get method is not possible. Every time you return to the previous page, the ajax method in the page will be executed as usual, but the background controller will not be requested to request the returned value. It 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();
}
Copy after login

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

I hope it can help those who encounter the same problem as me.

Note: The red part is the modified part

Related recommendations:

Example sharing of writing Ajax request function function in native JS

Problem with special symbols in get request

Detailed description of WeChat applet network request (GET request)

The above is the detailed content of AJAX request, solving the problem of invalid 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!