原生ajax
var xmlhttp = new XMLHttpRequest();xmlhttp.onreadystatechange = function () {if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {alert(xmlhttp.responseText)}};xmlhttp.open("GET", "BaseAjaxServlet", true);// 请求方式,servlet名,是否异步xmlhttp.send();
jq版本
- 简写
$.get("BaseAjaxServlet",{name:"田云",age:26},function (data) {alert(data);},"text");
- 全部参数
$.ajax({type: "GET",url: "BaseAjaxServlet",data: {name: "田云",age: 26},success: function (data) {alert(data);},error: function (jqXHR, textStatus, errorThrown) {/*弹出jqXHR对象的信息*/alert(jqXHR.responseText); // 服务器响应返回的文本信息alert(jqXHR.status); //HTTP状态码,比如常见的404,500等alert(jqXHR.readyState); //当前状态,0-未初始化,1-正在载入,2-已经载入,3-数据进行交互,4-完成alert(jqXHR.statusText); // 对应状态码的错误信息/*弹出其他两个参数的信息*/alert(textStatus); // 返回的状态 "timeout"(超时), "error"(错误), "abort"(中止), "parsererror"(解析错误),还有可能返回空值。alert(errorThrown); // 服务器抛出返回的错误信息},sync: true,dataType: "text"});$.ajax({type:"POST",url:url,contentType:"application/json ; charset=utf-8",dataType: 'json',data: JSON.stringify(data),success:function(data) {if (data.code == 0) {articleDetailSuccess(data.data);} else {layer.msg('接口返回错误');}},error: function(error) {layer.msg('请求接口失败');}});
axios
参考:https://www.yuque.com/tianyunperfect/ygzsw4/rgvcf2
这短短的一生我们最终都会失去,不放大胆一点,爱一个人、攀一座山、追一个梦!
