ajax封装
来源:互联网 发布:中国出口构成 知乎 编辑:程序博客网 时间:2024/05/18 19:45
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8">
<meta name="Keywords" content="关键字,关键词"/>
<meta name="Description" content="描述与简介"/>
</head>
<body>
<script>
/*
//HTTP状态码
var xhr = new XMLHttpRequest();
xhr.open( 'get' , 'data.php?user=afei&age=18&o=45' , true );
xhr.setRequestHeader('content-type' , 'application/x-www-form-urlencoded');
xhr.send('user=afei&age=18&o=45');
xhr.onreadystatechange = function(){
if ( xhr.readyState == 4 )
{
if ( xhr.status >= 200 && xhr.status < 300 )
{
alert( xhr.responseText );
}
else
{
alert( '访问出错了!错误信息:' + xhr.status );
};
}
};
*/
// type url data
ajax({
type : 'get',
url : 'ajax.txt',
data : {'user' : 'afei','age' : '18'},
success : function( goudan ){
alert( goudan );
},
error : function( s ){
alert( s );
}
});
//参数:
//{
// type :访问方式(get/post),默认'get' ,string
// url : 访问地址,必传,string
// data : 发送的数据,需要传数据才写,json
// success : 请求成功的回调函数,第一个形参代表返回的数据,function
// error : 请求失败的回调函数,第一个形参代表HTTP状态码,function
//}
function ajax(mJson){
var type=mJson.type||'get';
var url=mJson.url;
var data=mJson.data;
var dataStr='';
var success=mJson.success;
var error=mJson.error;
if (data){
for(var key in data )dataStr+=key+'='+data[key]+'&';
dataStr+='_='+new Date().getTime();
if(type.toLowerCase()=='get')url+='?'+dataStr;
};
var xhr=new XMLHttpRequest();
xhr.open(type,url,true);
xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
xhr.send(dataStr);
xhr.onreadystatechange=function(){
if (xhr.readyState==4)
(xhr.status>=200&&xhr.status<300)?(success&&success(xhr.responseText)):(error&&error(xhr.status));
};
};
</script>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8">
<meta name="Keywords" content="关键字,关键词"/>
<meta name="Description" content="描述与简介"/>
</head>
<body>
<script>
/*
//HTTP状态码
var xhr = new XMLHttpRequest();
xhr.open( 'get' , 'data.php?user=afei&age=18&o=45' , true );
xhr.setRequestHeader('content-type' , 'application/x-www-form-urlencoded');
xhr.send('user=afei&age=18&o=45');
xhr.onreadystatechange = function(){
if ( xhr.readyState == 4 )
{
if ( xhr.status >= 200 && xhr.status < 300 )
{
alert( xhr.responseText );
}
else
{
alert( '访问出错了!错误信息:' + xhr.status );
};
}
};
*/
// type url data
ajax({
type : 'get',
url : 'ajax.txt',
data : {'user' : 'afei','age' : '18'},
success : function( goudan ){
alert( goudan );
},
error : function( s ){
alert( s );
}
});
//参数:
//{
// type :访问方式(get/post),默认'get' ,string
// url : 访问地址,必传,string
// data : 发送的数据,需要传数据才写,json
// success : 请求成功的回调函数,第一个形参代表返回的数据,function
// error : 请求失败的回调函数,第一个形参代表HTTP状态码,function
//}
function ajax(mJson){
var type=mJson.type||'get';
var url=mJson.url;
var data=mJson.data;
var dataStr='';
var success=mJson.success;
var error=mJson.error;
if (data){
for(var key in data )dataStr+=key+'='+data[key]+'&';
dataStr+='_='+new Date().getTime();
if(type.toLowerCase()=='get')url+='?'+dataStr;
};
var xhr=new XMLHttpRequest();
xhr.open(type,url,true);
xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
xhr.send(dataStr);
xhr.onreadystatechange=function(){
if (xhr.readyState==4)
(xhr.status>=200&&xhr.status<300)?(success&&success(xhr.responseText)):(error&&error(xhr.status));
};
};
</script>
</body>
</html>
阅读全文