欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Ajax 给 XMLHttpReq.onreadystatechange传递参数

程序员文章站 2023-11-13 17:44:52
ajax 给 xmlhttpreq.onreadystatechange传递参数 通过: xmlhttp.onreadystatechange= function(){xx...
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>