用ajax时,向返回的处理函数发参数怎么做?给个思路
我有一个页面有5部分,每一部分都有保存按钮,我想实现按不同部分的保存按钮就发到不同的网页做保存操作,然后返回。
但是返回时我不知道是那一部分处理完成的返回。。。。
我不想做5个返回的处理函数,能不能给处理函数发参数?
代码如下:
function save_info(save_type,objbutton){
var queryString= " ";
switch(save_type)
{
case 0:
queryString=.....
url=.....
break;
.....
default:
.......
}
objbutton.value = "正在保存信息。。。 ";
objbutton.disabled =false;
var callback = processAjaxResponse;
executeXhr(callback,url,queryString);
}
function executeXhr(callback,url,queryString) {
var post_url=url;
var post_queryString=queryString;
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.open( "POST ", post_url, true);
req.setRequestHeader( "Content-Type ", "application/x-www-form-urlencoded ");
req.onreadystatechange = callback;
req.send(post_queryString);
} // branch for IE/Windows ActiveX version
else if (window.ActiveXObject) {
req = new ActiveXObject( "Microsoft.XMLHTTP ");
if (req) {
req.open( "POST ", post_url, true);
req.setRequestHeader( 'Content-type ', 'application/x-www-form-urlencoded;charset=GB2312 ');
req.onreadystatechange = callback;
req.send(post_queryString);
}
}
}
function processAjaxResponse() {
var i=save_type;
// only if req shows "loaded "
if (req.readyState == 4) {
// only if "OK "
if (req.status == 200) {
//alert(req.responseText);
var str;
alert( "保存成功! ")
}else {
document.getElementById( 'msg ').innerHTML= "网页出错 ";
}
}
else
{
document.getElementById( 'msg ').innerHTML= "找不到网页 ";
}
document.getElementById( 'b_01 ').value = " 保 存 ";
document.getElementById( 'b_01 ').disabled =false;
}
...
<input id= "b_01 " type= "button " style= "background-color:#CCCCCC;border:#CCCCCC solid 1px " name= "Submit " value= " 提 交 " donClick= ";save_info(0,this) " />
------解决方案--------