Detailed examples of Ajax features and garbled code problems

小云云
Release: 2023-03-18 13:24:02
Original
975 people have browsed it

The full name of ajax is asynchronous javascript and XML, which is asynchronous js and XML. It is a partial refresh, asynchronous operation. This article introduces to you the characteristics of ajax and the problem of garbled characters. Friends who are interested should take a look at it. I hope it can help you.

Everyone knows that ajax is single-threaded and synchronous in itself, but most people don’t know why a single-thread can be asynchronous. Let’s explain it to you by Zhang Yanni (not very official, but Very easy to understand):

Before I explain, let me tell you about the linear data structure. We can think of a single linear data structure as a line segment. A line segment has a beginning and a tail. Everyone knows that a single thread is a line segment. There is no front area in the head and no back area in the tail. Each element in the middle has two elements before and after it. When an element is missing, two elements will know and tell you at the same time. And it doesn’t even work without a single element.

The callback function passes function A as a parameter to function B, and function B executes function A. The most common uses of callback functions are the success() and error() functions we use when requesting data with ajax, as well as the first parameter in setInterval, which also uses callback functions. Callback function, callback function, the literal meaning of callback is to turn around and go back to walk that road again. So ajax turns around and walks again when the callback function success() or error is triggered, and then it is asynchronous. At the same time, ajax is asynchronous. Similar to it, there is es6's promise (asynchronous synchronous operation) .

The process of the browser is multi-process. As for why it is the same as why you call it XXX~

The second question is when the omnipotent front-end encounters ajax acquisition Everyone should be confused about why the background data is garbled.
-Maybe it’s a network speed problem, the network is too slow.

-Maybe the encoding format is not uniform, and your backend brothers have tricked you, hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha

#
//1.创建xmlhttpRequest对象
var xhr;
try{
   xhr = new XMLHttpRequest();
   console.log(5555)
}
catch(e){
//ie浏览器低版本兼容
   xhr = new ActiveXobject("Microsoft.XMLHTTP");
}
finally{
  console.log(1111)
}
// try{} catch{} finally{}在这里是处理异常的方法,用if{}else{}也ok 
//if(window.XMLHttpRequest){
 // xhr = new XMLHttpRequest();
//  }else{
  //ie浏览器低版本的兼容
 // xhr = new ActiveXobject("Microsoft.XMLHTTP")
//}
//2.建立异步连接
xhr.open("get","url/+string",true/false);//true是异步,false是同步
//xhr.open("post","url",true/false)
//3.发送异步请求
 xhr.send(null);//get
 xhr.send(string);//post
//4.获取返回数据
 xhr.onreadystatechange = function (){
  if((xhr.readystate)==4&&(xhr.status==200)){
   var data = xhr.responseText;
   var data = JSON.parse(data);
   show(data);
}
}
function show(data){
 document.getElementById("p1").innerHTML = data;
}
Copy after login

Related recommendations:


Example method of handwriting Ajax to implement asynchronous refresh

Example Analysis of Ajax asynchronous request technology

About the implementation method of loading waiting effect before Ajax returns data

The above is the detailed content of Detailed examples of Ajax features and garbled code problems. For more information, please follow other related articles on the PHP Chinese website!

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!