本文主要从使用ajax请求的步骤、ajax状态码和http响应状态码以及ajax封装三个方面阐述

一、使用ajax请求的步骤

// 一、创建 XMLHttpRequest 对象
var xhr = new XMLHttpRequest();
// 二、规定请求的类型、URL 以及是否异步处理请求。
// method:get/post
// url:请求地址
// async:true异步/false同步
xhr.open(method,url,async);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");//post方法必需
// 三、将请求发送到服务器
// string仅用于post请求
// get方法传参拼接在url后面即可
xhr.send(string);
// 四、接收响应有两种方法
// 1.新方法,只想要判断http的响应状态码
xhr.onload = function(){
if(xhr.status == 4){
console.log(xhr.responseText);
}
}
// 2.旧方法,想要同时判断Ajax的状态码和http的状态码
xhr.onreadystatechange = function(){
if(xhr.readyState == 200 && xhr.status == 4){
console.log(xhr.responseText);
}
}

二、ajax状态码(readystate)和http响应状态码(status)

1、ajax状态码(readystate)

2、http响应状态码(status)

三、ajax封装

//ajax方法的调用
ajax({
url:请求地址,
success:(res)=>{
//请求数据成功
// console.log(res);
}
})
// ajax封装的方法
function ajax({url,data,success,error,type}){
type = type || "get"; //该参数可选
data = data || {}; //该参数可选
let str = "";
//拼接参数
for(let i in data){
str += `${i}=${data[i]}&`;
}
str = str.slice(0,str.length-1);
//如果请求为get方式则拼接在请求地址后面
if(type === "get"){
var d = new Date();
url = url + "?" + str + "&__qft="+d.getTime();
}
let xhr = new XMLHttpRequest();
xhr.open(type, url, true);
//如果为post请求需要加入固定请求头部
if(type === "get"){
xhr.send();
}else if(type === "post"){
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(str);
}
xhr.onload = function(){
if(xhr.status === 200){
success(xhr.responseText);
}else{
error && error(xhr.status);
}
}
}

js-ajax方法详解以及封装的更多相关文章

  1. Js apply 方法 详解

    Js apply方法详解 我在一开始看到JavaScript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这 ...

  2. $.ajax()方法详解 jquery

    $.ajax()方法详解   jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...

  3. jQuery中 $.ajax()方法详解

    $.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Strin ...

  4. $.ajax()方法详解 ajax之async属性 【原创】详细案例解剖——浅谈Redis缓存的常用5种方式(String,Hash,List,set,SetSorted )

    $.ajax()方法详解   jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...

  5. Js apply方法详解,及其apply()方法的妙用

    Js apply方法详解 我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这 ...

  6. js数组方法详解

    Array对象的方法-25个 /*js数组方法详解 */ /* * 1 concat() 用于连接多个数组或者值-------------- * 2 copyWithin() 方法用于从数组的指定位置 ...

  7. jQuery - Ajax ajax方法详解

    $.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Strin ...

  8. jquery中的ajax方法详解

    定义和用法ajax() 方法通过 HTTP 请求加载远程数据.该方法是 jQuery 底层 AJAX 实现.简单易用的高层实现见 $.get, $.post 等.$.ajax() 返回其创建的 XML ...

  9. JS reduce()方法详解,使用reduce数组去重

     壹 ❀ 引 稍微有了解JavaScript数组API的同学,对于reduce方法至少有过一面之缘,也许是for与forEach太强大,或者filter,find很实用,在实际开发中我至始至终没使用过 ...

随机推荐

  1. SpringBatch异常To use the default BatchConfigurer the context must contain no more thanone DataSource

    SpringBoot整合SpringBatch项目,已将代码开源至github,访问地址:https://github.com/cmlbeliever/SpringBatch 欢迎star or fo ...

  2. Js调用Android回调处理

    通常在混合app中经常会使用js调用native的方法,一般是: window.nativeApp.call(XXX); 直接调用native方法,对于简单的处理倒是可以,如果需要回调呢?期待的方式是 ...

  3. Linux系统中如何升级pip

    问题提出:在Linux系统下安装python的logging库时提示以下信息 经过一番折腾,定位在pip版本过低和setuptools版本过低上 一.Linux下更新包 sudo python3 -m ...

  4. React组件proptypes, ref

    一.使用props.children访问嵌套数据 import React from 'react'; class Button extends React.Component { render () ...

  5. wepy 小程序开发(interceptor拦截器 && WXS)

    WePY全局拦截器可对原生API的请求进行拦截. import wepy from 'wepy'; export default class extends wepy.app { constructo ...

  6. Redux:with React(一)

    作者数次强调,redux和React没有关系(明明当初就是为了管理react的state才弄出来的吧),它可以和其他插件如 Angular, Ember, jQuery一起使用.好啦好啦知道啦.Red ...

  7. 博客营销(Blog Marketing)

    一.什么是博客营销 博客营销(Blog Marketing)的概念可以说并没有严格的定义,简单来说,就是利用博客这种网络应用形式开展网络营销.要说明什么是博客营销,首先要从什么是博客说起. 博客(Bl ...

  8. search(16)- elastic4s-内嵌文件:nested and join

    从SQL领域来的用户,对于ES的文件关系维护方式会感到很不习惯.毕竟,ES是分布式数据库只能高效处理独个扁平类型文件,无法支持关系式数据库那样的文件拼接.但是,任何数据库应用都无法避免树型文件关系,因 ...

  9. eclipse——管理远程资源的缓存,例如从Internet下载的资源。

    原文:Manage the cache of remote resources,such as those downloaded from the internet.

  10. 一、CentOS6.8安装MySQL5.6

    一.官网下载rpm安装包 https://dev.mysql.com/downloads/ 版本选中如图中红色框 二.卸载旧mysql 1.检查是否安装有mysql rpm -qa | grep -i ...