Ajax同步与异步传输的示例代码_javascript技巧

js教程评论352 views阅读模式
复制代码 代码如下:

//同步传输模式

function RequestByGet(nProducttemp,nCountrytemp)

{

var xmlhttp

if (window.XMLHttpRequest)

{

//isIE = false;

xmlhttp = new XMLHttpRequest();

}

else if (window.ActiveXObject)

{

//isIE = true;

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

//Web page location.

var URL="http://www.jb51.net/;

xmlhttp.open("GET",URL, false);

//xmlhttp.SetRequestHeader("Content-Type","text/html; charset=Shift_JIS")

xmlhttp.send(null);

var result = xmlhttp.status;

//OK

if(result==200)

{

document.getElementById("div_RightBarBody").innerHTML=xmlhttp.responseText;

}

xmlhttp = null;

}

//异步传输模式

var xmlhttp

function RequestByGet(nProducttemp,nCountrytemp)

{

if (window.XMLHttpRequest)

{

//isIE = false;

xmlhttp = new XMLHttpRequest();

}

else if (window.ActiveXObject)

{

//isIE = true;

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}

//Web page location.

var URL="http://www.jb51.net/";

xmlhttp.open("GET",URL, true);

xmlhttp.onreadystatechange = handleResponse;

//xmlhttp.SetRequestHeader("Content-Type","text/html; charset=UTF-8")

xmlhttp.send(null);

}

function handleResponse()

{

if(xmlhttp.readyState == 4 && xmlhttp.status==200)

{

document.getElementById("div_RightBarBody").innerHTML=xmlhttp.responseText;

xmlhttp = null;

}

}

企鹅博客
  • 本文由 发表于 2020年8月21日 18:03:03
  • 转载请务必保留本文链接:https://www.qieseo.com/420545.html

发表评论