Ajax 給 XMLHttpReq.onreadystatechange傳遞參數_AJAX相關

來源:互聯網
上載者:User
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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.