jQuery ajax

前后端进行数据交互 jquery ajax简直不要太方便

整理jquery ajax最常用的几个属性如下,方便查看:

$.ajax({

  type:"post"/"get",//请求方式 ,常用的有"post" 或 "get", 默认为 "get",post一般用于储存数据,get用于读取数据

  url:"路径", //数据接口

  async:true/false,//默认值为true,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。

  data:obj ,//发送到后台的数据,可以采用对象的形式

  dataType: ,//预期后台传过来的数据类型

  success:function(){},//当请求成功之后调用。传入返回后的数据,以及包含成功代码的字符串

  error:function(){},//在请求出错时调用

});

原生的js来进行ajax请求,步骤如下:

(1)创建xhr实例

var xhr = new XMLHttpRequest();

(2)绑定监听 readyState

xhr.onreadystatechange = function(){

  if(xhr.readyState === 4 && xhr.status ===200){

    console.log(xhr.response);

  }

}

(3)open 设置请求的参数及url

xhr.open("get/post","url");

(4)发送

xhr.send();

原文地址:https://www.cnblogs.com/lihuijuan/p/8793278.html