原生JS处理
创建 XMLHttpRequest对象
var xmlhttp;if(window.XMLHttpRequest){// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码xmlhttp = new XMLHttpReques();}else{// IE6, IE5 浏览器执行代码xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
发送请求
基本格式
// method为get或者post,url为请求地址,async为异步还是同步xmlhttp.open(method, url, async)xmlhttp.send()
GET
// 格式xmlhttp.open("get","url", true);xmlhttp.send();
POST
// 简单请求xmlhttp.open("post","url",true);xmlhttp.send();// 添加HTTP头xmlhttp.open("post","url",true);xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlhttp.send("fname=zhang&lname=san");//携带参数
请求处理
xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState == 4 && xmlhttp.status == 200){//数据处理}}
jQuer处理
$.ajax({type: "get",url: "数据接口地址",data: {// query传递参数},dataType:"json",success:function(res){//请求成功后,处理返回数据},error:function(res){//请求失败后,处理返回的结果}})
