前言

ajax的例子和解析

ajax API地址
https://www.w3school.com.cn/jquery/ajax_ajax.asp

jquery ajax在线文档
https://jquery.cuishifeng.cn/jQuery.Ajax.html

jquery在线文档
https://jquery.cuishifeng.cn/

常用ajax的简单示例

$.ajax({type: 'POST', //类型url: '/user/save', //接口地址data: {name: 'wangduoyu',age:10,address:'北京'},//参数dataType: 'json', //数据类型success: function (result){//请求成功console.log(JSON.parse(result));},error:function(){ //请求失败alert("请求失败");}});

完整ajax示例

$.ajax({url : "abc.shtml",//连接的服务端timeout:5000,//超时时间(毫秒)async:false,//同步or异步请求data : {"key" : value},//提交的数据type : "post",//提交的方式GET/POSTdataType : "json",//返回的数据类型xhrFields: { withCredentials: true },//涉及到cookie跨域时才需要用到success : function(data) {},//返回数据、没有错误执行的代码error : function() {},//出错时执行的代码,一般是:服务端连接失败beforeSend : function() {},//在url请求前执行的代码complete : function() {}//当请求完成之后调用这个函数,无论成功或失败。传入 XMLHttpRequest 对象,以及一个包含成功或错误代码的字符串});//ajax