Home > Web Front-end > JS Tutorial > How to solve garbled characters when using Ajax

How to solve garbled characters when using Ajax

php中世界最好的语言
Release: 2018-03-31 11:34:48
Original
1670 people have browsed it

This time I will show you how to solve the problem of garbled characters when using Ajax. What are the precautions to solve the problem of garbled characters when using Ajax. The following is a practical case, let's 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.

Callback function is to pass 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 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 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, 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
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Ajax method to implement Form form submission


How jQuery verifies Ajax submission form parameters

The above is the detailed content of How to solve garbled characters when using Ajax. 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