ajax
ajax的五大步骤
创建xmlhttprquest对象用于在后台与页面交换数据
var xmlhttp;
function creatxmlhttprequest(){
if(window.activeXObject){
xmlhttp=activeXObjrct("Microsoft.xmlHttp")//针对于ie
}else if(window.createxmlhttprequest){
xmlhttp=window.createxmlhttprequest()//普通浏览器
}
}
使用get获取ajax的方法
get方法只能获取并不能上传,并且传输的最大2kb
function get(url){
createxmlhttprequest()
xmlhttp.open("get","url")}//使用get获取时,参数可以写在url?后面用=相接
xmhttp.send(null)//参数为null因为使用get时参数已经在url中了
//readystate是xmlhttprequest对象的一种状态其中的数值状态如下:
0: 请求未初始化-->1: 服务器连接已建立-->2: 请求已接收-->3: 请求处理中-->4: 请求已完成,且响应
已就绪
当readystate的数值为4并且status为200表示响应已就绪
//onreadystatechange是当readystate的值改变就会触发这个事件
xmlhttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.readyState==200)
header.innerHTML=responseText//responsetext是当请求完成时,后台发送给前台的数据
}
使用post获取ajax的方法
post方法与get基本一样,post传输量不受限制并且post上传与获取都能做到
用法:
post的发送请求参数需要用变量保存起来作为send()的参数,不能写在url的后面
jq的ajax请求
$.ajax({
type:"get/post",
url:“url”,
async:“true”//异步,
success:function(){
//成功之后的执行函数
//传回来有可能时josn对象或字符串所以用jq.parsejosn()
var res = $.parseJSON(res);
}})