前端开发那些事儿

ajax和axios使用和方法

2021-01-07  本文已影响0人  安掌门dear

心里最崇拜的那个人,不必变成那个人,而是用那个人的精神和方法,去变成你自己
----本文来自拉勾大前端

Ajax

ajax请求

原生Ajax详解

// 1.创建一个 XMLHttpRequest 类型的对象 
var xhr = null;
// 兼容写法
if (window.XMLHttpRequest) {
  // 标准浏览器
  xhr = new XMLHttpRequest();
} else {
  // IE 6 浏览器
  xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
// 2.open() 方法开启请求
// xhr.open("GET","https://jsonplaceholder.typicode.com/users?id=1");
xhr.open("POST","https://jsonplaceholder.typicode.com/users");
// 设置请求头
// 一般 get 方法不需要设置,而 post 方法必须设置
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
// 3.send() 方法发送一次请求
// 如果是 get 方法请求,不需要再 send 中传参数,它如果想传参数,直接写在网址上
// xhr.send(null);
xhr.send("name=harry&age=19");

xhr.onreadystatechange = function () {
  // 通过判断 xhr 的 readyState ,确定此次请求是否完成
  if (this.readyState === 4) {
    console.log(this.responseText)
  }
}

同步和异步

数据格式

json-server

原生Ajax--GET请求

var xhr =new XMLHttpRequest();
//发送get请求
//在open中传参
xhr.open("GET","http://localhost:3000/posts?id=1");
xhr.send(null);
xhr.onreadystatechange = function(){
  if(this.readyState === 4){
    console.log(this.responseText);
  }
}

原生Ajax--POST请求

封装AJAX函数

// 封装自己的 Ajax 函数
/**
  * 参数1:{string}    method  请求方法
  * 参数2:{string}    url     请求地址
  * 参数3:{Object}    params  请求参数
  * 参数4:{function}  done    请求完成后执行的回调函数
*/ 
function ajax(method, url, params, done) {
  // 统一将方法中的字母转大写,便于后面判断
  method = method.toUpperCase();
  // 书写 IE 6 的兼容
  var xhr = window.XMLHttpRequest  
  ? new XMLHttpRequest() 
  : new ActiveXObject("Microsoft.XMLHTTP");
  // 将对象格式的参数转为 urlencoded的格式
  var pairs = [];
  for (var k in params) {
    pairs.push(k + "=" + params[k]);
  }
  var str = pairs.join("&");
  // 判断是否是 get 方法,需要更改 url 的值
  if (method === "GET") {
    url += "?" + str;
  }
  // 创建打开一个连接
  xhr.open(method, url);
  var data = null;
  // 如果是 post 方法,需要设置请求头,还有请求体
  if (method === "POST") {
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    data = str;
  }
  xhr.send(data);
  // 执行回调函数
  xhr.onreadystatechange = function () {
    if (this.readyState !== 4) return;
    // 执行外部传进来的回调函数即可
    // 需要用到响应体
    done(JSON.parse(this.responseText));
  }
}
// 调用函数
ajax("GET", "http://localhost:3000/users",{"id": 1},function (data) {
  console.log(data);
});
ajax("POST", "http://localhost:3000/users",{"name": "john","age": 19,"class": 2},function (data) {
  console.log(data);
});

jQuery中ajax

$.ajax()

$.get()

$.post()

 $.post("http://localhost:3000/posts",{"name": "dxa"},function(data){
   console.log(data)
 })

其他数据类型

// put 请求,更改数据
$.ajax({
  url: "http://localhost:3000/comments/4",
  type: "put",
  dataType: "json",
  data: {"content": "good", "postId": 2},
  success: function (data) {
    console.log(data)
  }
})
// delete 请求,删除数据
$.ajax({
  url: "http://localhost:3000/comments/5",
  type: "delete",
  success: function (data) {
    console.log(data)
  }
})

Axios

Axios API

常用配置选项

全局默认配置

onload/onprogress

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:3000/posts");
xhr.onload = function () {
  console.log("load",this.readyState)
}
xhr.onprogress = function (e) {
  console.log("progress",this.readyState)
  // 在周期性请求过程中,接收到的数据的个数
  console.log(e.loaded);
  // 接收数据的总个数
  console.log(e.total);
}
xhr.send(null);

response属性

上一篇 下一篇

猜你喜欢

热点阅读