Ajax 給 XMLHttpReq.onreadystatechange傳遞參數(shù)
更新時(shí)間:2014年05月13日 11:03:43 作者:
這篇文章主要介紹了Ajax如何給XMLHttpReq.onreadystatechange =函數(shù)傳遞參數(shù),需要的朋友可以參考下
Ajax 給 XMLHttpReq.onreadystatechange傳遞參數(shù)
通過:
xmlhttp.onreadystatechange= function(){xx(123)};
or xmlhttp.onreadystatechange= new Function("xx(123)"); 就可以了。
Ajax 給 XMLHttpReq.onreadystatechange傳遞參數(shù)
通過:
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='....';
}
}
}
一個(gè)小測(cè)試的例子:
<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>
通過:
xmlhttp.onreadystatechange= function(){xx(123)};
or xmlhttp.onreadystatechange= new Function("xx(123)"); 就可以了。
復(fù)制代碼 代碼如下:
Ajax 給 XMLHttpReq.onreadystatechange傳遞參數(shù)
通過:
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='....';
}
}
}
一個(gè)小測(cè)試的例子:
復(fù)制代碼 代碼如下:
<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:
復(fù)制代碼 代碼如下:
<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訪問不斷變化的session的值不一致以及HTTP協(xié)議中的GET、POST的區(qū)別
- 通過XMLHttpRequest和jQuery實(shí)現(xiàn)ajax的幾種方式
- node.js+Ajax實(shí)現(xiàn)獲取HTTP服務(wù)器返回?cái)?shù)據(jù)
- javascript學(xué)習(xí)筆記(七)Ajax和Http狀態(tài)碼
- 解析ajax核心XMLHTTPRequest對(duì)象的創(chuàng)建與瀏覽器的兼容問題
- AJAX(XMLHttpRequest.status)狀態(tài)碼
- 動(dòng)態(tài)表單驗(yàn)證的操作方法和TP框架里面的ajax表單驗(yàn)證
相關(guān)文章
ajax請(qǐng)求post和get的區(qū)別以及get post的選擇
這篇文章主要介紹了ajax請(qǐng)求post和get的區(qū)別以及get post的選擇,需要的朋友可以參考下2014-06-06
Ajax+Asp源代碼]讀取數(shù)據(jù)庫(kù)內(nèi)容的表格(沒有用框架)
Ajax+Asp源代碼]讀取數(shù)據(jù)庫(kù)內(nèi)容的表格(沒有用框架)...2006-11-11
jQuery Ajax使用心得詳細(xì)整理及注意事項(xiàng)
jQuery Ajax相關(guān)文章想必大家在網(wǎng)上面已經(jīng)看到了很多,本文主要目的是整理jQuery Ajax相關(guān)內(nèi)容,感興趣的朋友可以了解下,或許本文對(duì)你有所幫助2013-02-02
H5移動(dòng)開發(fā)Ajax上傳多張Base64格式圖片到服務(wù)器
這篇文章主要為大家詳細(xì)介紹了H5移動(dòng)開發(fā)Ajax上傳多張Base64格式圖片到服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Ajax加載外部頁面彈出層效果實(shí)現(xiàn)方法
這篇文章主要介紹了Ajax加載外部頁面彈出層效果實(shí)現(xiàn)方法,涉及Ajax加載彈出層的實(shí)現(xiàn)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05
詳解Ajax跨域(jsonp) 調(diào)用JAVA后臺(tái)
本篇文章主要介紹了詳解Ajax跨域(jsonp) 調(diào)用JAVA后臺(tái) ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05

