Home  >  Article  >  Web Front-end  >  Ajax characteristics and garbled code problems (graphic tutorial)

Ajax characteristics and garbled code problems (graphic tutorial)

亚连
亚连Original
2018-05-22 10:30:181081browse

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 the characteristics of ajax and the problem of garbled characters. Friends who are interested should take a look.

The full name of ajax is asynchronous javascript and XML, which is asynchronous js and XML. It is a partial refresh, asynchronous operation.

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 browser process 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 to obtain background data Everyone should be confused about what the garbled code is.
-Maybe it’s a network speed problem, the network is too slow.

-Maybe the encoding format is not uniform, your backend brothers have cheated you, hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha #

//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;
}

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Summary of the advantages and disadvantages of Ajax

Detailed explanation of the steps to use Ajax

Detailed explanation of how Ajax() interacts with the background

The above is the detailed content of Ajax characteristics and garbled code problems (graphic tutorial). 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