/** * 得到ajax对象 */function getajaxHttp() { var xmlHttp; try { //chrome, Firefox, Opera 8.0+, Safari xmlHttp = new XMLHttpRequest(); } catch (e) { // Internet Explorer try { //IE5,6 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } c
/**
* 得到ajax对象
*/
function getajaxHttp() {
var xmlHttp;
try {
//chrome, Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
//IE5,6
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
//IE7以上
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("您的浏览器不支持AJAX!");
return false;
}
}
}
return xmlHttp;
}
/**
* 发送ajax请求
* url--url
* methodtype(post/get)
* con (true(异步)|false(同步))
* parameter(参数)
* functionName(回调方法名,不需要引号,这里只有成功的时候才调用)
* (注意:这方法有二个参数,一个就是xmlhttp,一个就是要处理的对象)
* obj需要到回调方法中处理的对象
*/
function ajaxrequest(url,methodtype,con,parameter,functionName,obj){
var xmlhttp=getajaxHttp();
xmlhttp.onreadystatechange=function(){
// readyState值说明
// 0,初始化,XHR对象已经创建,还未执行open
// 1,载入,已经调用open方法,但是还没发送请求
// 2,载入完成,请求已经发送完成
// 3,交互,可以接收到部分数据
// status值说明
// 200:成功
// 404:没有发现文件、查询或URl
// 500:服务器产生内部错误
if(xmlhttp.readyState==4&& XHR.status == 200){
//HTTP响应已经完全接收才调用
functionName(xmlhttp,obj);
}
};
xmlhttp.open(methodtype,url,con);
xmlhttp.send(parameter);
}
//这就是参数
function createxml(){
var xml="<user><userid>asdfasdfasdf<\\/userid><\\/user>";//"\\/"这不是大写V而是转义是左斜杠和右斜杠
return xml;
}
//这就是参数
function createjson(){
var json={id:0,username:"好人"};
return json;
}
function c(){
alert("");
}
//测试
ajaxrequest("http://www.baidu.com","post",true,createxml(),c,document);
