美国松石有哪些:关于ajax一个例子的问题

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/09 14:54:26
var req;
function loadXMLDoc(url,thisvalue) {
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send();
}
}
return true;
}
//////////////////
function processReqChange() {
// only if req shows "loaded"
//如何在这里得到thisvalue的值
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {

} else {
}
}
}
<input type="hidden" id="varValue" name="varValue" value="">

此中方法是可以,我以前也这样做过,也用cookie实现过,但是我想直接在loadXMLDoc()中直接通过参数传递,有其他方法么?

你是想要取得页面返回的内容是吗?
使用 responseText 方法
如:req.responseText
-----------------------

如果单单想得到loadXMLDoc 中的thisvalue值的话,可以在这个JS里加一个全局变量,在loadXMLDoc 过程中赋值,在processReqChange过程中取值,当然最好是可以将它封装成一个类,这样thisvalue就可以当成属性来使用了。

加一个:<input type="hidden" id="varValue" name="varValue" value="">
然后:

function loadXMLDoc(url,thisvalue) {
中加入:
document.getElementById("varValue").value=thisvalue;


function processReqChange() {
中引用为:
thisvalue=document.getElementById("varValue").value;