Ajax 给 XMLHttpReq.onreadystatechange传递参数

初夏的脚步刚刚走来,人们纷纷把厚重的单衣脱掉,换上了凉爽的短袖,又把旅游鞋换成了颜色各不相同的夹鞋,有淡绿色的;有洁白的;还有深蓝色的……
Ajax 给 XMLHttpReq.onreadystatechange传递参数 通过:
xmlhttp.onreadystatechange= function(){xx(123)};
or xmlhttp.onreadystatechange= new Function("xx(123)"); 就可以了。
 
Ajax 给 XMLHttpReq.onreadystatechange传递参数 通过:
xmlhttp.onreadystatechange= function(){xx(123)};
or
xmlhttp.onreadystatechange= new Function("xx(123)");
就可以了。
m=document.getElementsByName("text8");
v=m[i];
XMLHttpReq.onreadystatechange=function(){proce(v)};
----------------------------------------------
function proce(v)
{
if(XMLHttpReq.readyState==4)
{
if(XMLHttpReq.status==200)
{
var res=XMLHttpReq.responseXML.getElementsByTagName("content")[0].firstChild.data;
v.value=res;
}
else
{
v.value='....';
}
}
}

一个小测试的例子:
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>test Ajax</title>
<mce:script language="javascript"><!--
function getXMLHttpRequest() {
var xmlHttpRequest;
try
{
xmlHttpRequest=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
xmlHttpRequest=new ActiveXObjec("Msxml2.XMLHTTP");
}
catch (e)
{
throw new Error('Unable to create XMLHttpRequest.')
}
}
}
return xmlHttpRequest;
}
/*
function test() {
var xhr = getXMLHttpRequest();
var url="http://211.87.235.108:5000/sensor.xml";
xhr.open("GET",url);
xhr.send(null);
xhr.onreadystatechange = function(){
if(4==xhr.readyState)
{
if(200==xhr.status) {
//var xmldoc=xmlHttpRequest.responseXML;
var xmldoc=xhr.responseText;
document.getElementById("disp").innerHTML=xmldoc;
}
else {
//alert(xhr.status);
}
}
}; xhr.onstatechange = function() {
handleXMLHttpRequest(xhr) ;
};
}
*/ function handleXMLHttpRequest( xmlHttpRequest) {
if(4==xmlHttpRequest.readyState)
{
if(200==xmlHttpRequest.status){
//var xmldoc=xmlHttpRequest.responseXML;
var xmldoc=xmlHttpRequest.responseText;
document.getElementById("disp").innerHTML=xmldoc;
}
else {
alert(xmlHttpRequest.status);
}
}
}
function doXMLHttpRequest() {
var xhr = getXMLHttpRequest();
var url="http://localhost:5000/sensor.xml";
xhr.open("GET",url);
xhr.send(null);
xhr.onreadystatechange =function (){
handleXMLHttpRequest(xhr);
};
} function myrefresh() {
window.location.reload();
}
setTimeout('myrefresh()',1000); //指定1秒刷新一次
// --></mce:script> </head>
<mce:style><!--
body {font-size:12px;}
--></mce:style><style mce_bogus="1"> body {font-size:12px;}</style>
<body onLoad="doXMLHttpRequest()">
<div id="disp">
</div>
</body>
</html>

用例2:
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title> My Documents </title>
<mce:script language="javascript"><!--
function getXMLHttpRequest() {
var xmlHttpRequest;
try
{
xmlHttpRequest=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try
{
xmlHttpRequest=new ActiveXObjec("Msxml2.XMLHTTP");
}
catch (e)
{
throw new Error('Unable to create XMLHttpRequest.')
}
}
}
return xmlHttpRequest;
}
function test() {
var xhr = getXMLHttpRequest();
var url="http://211.87.235.108:5000/sensor.xml";
xhr.open("GET",url);
xhr.send(null);
xhr.onreadystatechange = function(){
if(4==xhr.readyState)
{
if(200==xhr.status) {
//var xmldoc=xmlHttpRequest.responseXML;
var xmldoc=xhr.responseText;
document.getElementById("disp").innerHTML=xmldoc;
}
else {
//alert(xhr.status);
}
}
};
}
/*
function handleXMLHttpRequest( xmlHttpRequest) {
alert(xmlhttpRequest + 2);
if(4==xmlHttpRequest.readyState)
{
if(200==xmlHttpRequest.status){
//var xmldoc=xmlHttpRequest.responseXML;
var xmldoc=xmlHttpRequest.responseText;
document.getElementById("disp").innerHTML=xmldoc;
}
else {
alert(xmlHttpRequest.status);
}
}
} function doXMLHttpRequest() {
var xhr = getXMLHttpRequest();
var url="http://211.87.235.108:5000/sensor.xml";
xhr.open("GET",url);
xhr.send(null);
xhr.onreadystatechange = handleXMLHttpRequest();
}
*/ function myrefresh() {
window.location.reload();
}
// setTimeout('myrefresh()',1000); //指定1秒刷新一次
// --></mce:script> </HEAD>
<mce:style><!--
body {font-size:12px;}
--></mce:style><style mce_bogus="1"> body {font-size:12px;}</style>
<body onLoad="test()">
<div id="disp">
</div>
</body>
</html>

本文Ajax 给 XMLHttpReq.onreadystatechange传递参数到此结束。我们必须拿我们所有的,去换我们所没有的。小编再次感谢大家对我们的支持!

标签: Ajax XMLHttpReq