javascript中使用ajax时设置回调函数,我想向回调函数中传参?该怎么传参?
getCitysByParentId是回调函数,我现在想往getCitysByParentId这个回调函
数中传liId这个参数进去,该怎么处理?
<script type="text/javascript">
var xmlHttpRequest;
function createXmlHttpRequest() {
//判断不同浏览器,采用不同方式创建XMLHttpRequest对象
if (window.ActiveXObject) {
//IE浏览器
try {
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
return xmlHttpRequest;
} else if (window.XMLHttpRequest) {
//其他浏览器如Firefox、Chrome等等
return new XMLHttpRequest();
}
}
//我是回调函数
function getCitysByParentId(currentLiId) {
//做一些事情,代码省略...
//回调函数中要使用currentLiId这个参数,所以需要外界传一个参数进来
}
function getCitys(cityId, liId) {
//1.创建XMLHttpRequest对象
xmlHttpRequest = createXmlHttpRequest();
//getCitysByParentId是回调函数,我现在想往getCitysByParentId这个回调函
数中传liId这个参数进去,该怎么处理?
//2.设置回调函数
xmlHttpRequest.onreadystatechange = getCitysByParentId;
/*
getCitysByParentId是回调函数,我现在想往getCitysByParentId这个回调函
数中传liId这个参数进去,该怎么处理?
*/
//请求的目标地址
var url = "${pageContext.request.contextPath}/JsonCity?provinceId="
+ cityId;
//3.初始化XMLHttpRequest组件
//addTimestampParameter(url)函数不太好的就是,URL本身可能带有一些参数,那就要判断是加"?"还是加"&"了
//xmlHttpRequest.open("GET", addTimestampParameter(url), true);
xmlHttpRequest.open("GET", url, true);
//xmlHttpRequest.open("POST", url, true);
//推荐使用下面这种方式,简洁灵活,不影响URL参数
xmlHttpRequest.setRequestHeader('If-Modified-Since', '0');
//4.发送请求
xmlHttpRequest.send(null);
//显示加载图片
showLoading(document.getElementById(liId),
"${pageContext.request.contextPath}/img/loading1.gif");
}
</script>