Home  >  Article  >  Web Front-end  >  jquery1.8 version uses ajax to implement problem analysis and solutions for WeChat calls

jquery1.8 version uses ajax to implement problem analysis and solutions for WeChat calls

亚连
亚连Original
2018-05-24 14:52:282160browse

This article mainly introduces the analysis and solutions to the problems that arise when using ajax to implement WeChat calls in jquery1.8 version. Friends who need it can refer to it

Let me first tell you the background of the problem: Recently I am working on a project to develop a certain function of WeChat. I am using asp.net development and jquery version 1.8.0.

Click a button in WeChat to trigger an event, call ajax to interact with the server, and the callback function uses error.

Analysis reasons: The first thing I thought of was that there was a problem with the returned data type, because jquery versions above 1.4 are very strict on the json format and need to comply with {"target":true, "message":"success"} in this format. The response object was analyzed using the JSON.stringify() function. The results were consistent and the cause was eliminated.

Since ajax uses error, I started to analyze the parameters of the error function, which include XMLHttpRequest, textStatus, and errorThrown. I found that XMLHttpRequest.status is equal to 0 and XMLHttpRequest.readyState is equal to 0, indicating that there is an XMLHttpRequest object in the form but it has not been initialized. Next, I started to study how to initialize the XMLHttpRequest object.

Solution:

var xmlHttpRequest;
$(function(){
if(window.XMLHttpRequest){
xmlHttpRequest=new XMLHttpRequest();
}else{
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpRequest.open("GET","AjaxServlet",true);
});

This can solve the problem described above. Pay attention to the general Ajax access on the PC side. There will be no problem because browsers on PCs have built-in XMLHttpRequest objects, but in WeChat, if this phenomenon occurs, you may need to manually build and initialize it.

ps: The Android version of WeChat comes with a browser and the IE6 browser ajax request abort error handling

Binds a click event to the page element to trigger the ajax request , in the built-in browser of Android WeChat and IE6, the request is often interrupted and the error type "abort" is returned. In other browsers, everything is normal.

When using Fiddler2 and httpWatch to monitor requests in IE6, "aborted" often appears, which takes a whole weekend. . . . .

No more nonsense....

Solution: Add onclick='return false;'

The above is me on the label I compiled it for everyone, I hope it will be helpful to everyone in the future.

Related articles:

Improve ajax list request experience based on h5 history

Briefly talk about AJAX core objects

Detailed explanation of examples of ajax data transmission methods

##

The above is the detailed content of jquery1.8 version uses ajax to implement problem analysis and solutions for WeChat calls. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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