ajax请求步骤 和 简易封装

ajax请求的五个步骤

一. 原生中的ajax

  1. 创建异步对象
    var xhr = new XMLHttpRequest();
    
  2. 设置 请求行 open(请求方式,请求url)
    // get请求如果有参数就需要在url后面拼接参数,
    xhr.open("get","validate.php?username="+name)
    // post如果有参数,就在请求体中传递 
    xhr.open("post","validate.php");
    
  3. 设置请求(GET方式忽略此步骤)头:setRequestHeader()
    // 1.get不需要设置
    // 2.post需要设置请求头:Content-Type:application/x-www-form-urlencoded
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    
  4. 设置请求体 send()
    // 1.get的参数在url拼接了,所以不需要在这个函数中设置
    xhr.send(null)
    // 2.post的参数在这个函数中设置(如果有参数)
    xhr.send("username="+name);
    
  5. 让异步对象接收服务器的响应数据
    // 一个成功的响应有两个条件:1.服务器成功响应了 2.异步对象的响应状态为4(数据解析完毕可以使用了)
    xhr.onreadystatechange = function(){ 
    if(xhr.status == 200 && xhr.readyState == 4){ 
     console.log(xhr.responseText);
     }
    
  • ajax请求get案例 :
    let xhr = new XMLHttpRequest();
    xhr.open("GET", "https://cnodejs.org/api/v1/topics");
    xhr.onreadystatechange = function () {
      if (xhr.status == 200 && xhr.readyState == 4) {
        console.log(JSON.parse(xhr.responseText));
      }
    };
    xhr.send();
    
  • ajax请求post案例:
    var xhr = new XMLHttpRequest();
    xhr.open("post","validate.php");
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xhr.send("username="+name);
    xhr.onreadystatechange = function(){
    	// 判断服务器是否响应,判断异步对象的响应状态
    	if(xhr.status == 200 && xhr.readyState == 4){
    		console.log(JSON.parse(xhr.responseText));
    	}
     }
    
简易封装
function getSend(url,callback){
  let xhr = new XMLHttpRequest();
  xhr.open("GET", url);
  xhr.onreadystatechange = function () {
    if (xhr.status == 200 && xhr.readyState == 4) {
      callback(JSON.parse(xhr.responseText));
    }
  };
  xhr.send();
}
getSend("https://cnodejs.org/api/v1/topics",(res)=>{
    console.log(res)
})
// Promise形式
function pGetSend(url) {
  return new Promise((resolve,reject)=>{
    let xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.onreadystatechange = function () {
      if (xhr.status == 200 && xhr.readyState == 4) {
        resolve(JSON.parse(xhr.responseText));
      }else{
        reject("错误信息!!")
      }
    };
    xhr.send();
  })
}
pGetSend("https://cnodejs.org/api/v1/topics").then(res=>{
  console.log(res)
})

二. jQuery中的ajax

$.ajax({
	type:"get",// get或者post
	url:"abc.php",// 请求的url地址
	data:{},//请求的参数
	dataType:"json",//json写了jq会帮我们转换成数组或者对象 他已经用JSON.parse弄好了 
	timeout:3000,//3秒后提示错误
	beforeSend:function(){ 
	// 发送之前就会进入这个函数
	// return false 这个ajax就停止了不会发 如果没有return false 就会继续
	},
	success:function(data){ 
		// 成功拿到结果放到这个函数 data就是拿到的结果
	},
	error:function(){
		//失败的函数
	},
	complete:function(){
		//不管成功还是失败 都会进这个函数
	}
})
// 常用
$.ajax({
	type:"get",
	url:"",
	data:{},
	dataType:"json",
	success:function(data){
	}
})

$.ajax() 都可以发
$.post(url,data,success,datatype):本质上只能发送post请求
$.get(url,data,success,datatype):本质上只能发送get请求

本文转载原文地址 : https://blog.csdn.net/Hulu_IT/article/details/89363145

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值