ajax hello world

前端之家收集整理的这篇文章主要介绍了ajax hello world前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

两个文件,一个db.asp,一个weather.html

db.asp 就是返回温度值

<%

dim city

city = Request.QueryString("city")

if city = "guangzhou" then

Response.Write("31")

elseif(city = "beijing") then

Response.Write("21")

else

Response.Write("na")

end if

%>


weather.html是主要程序

<html>

<body>

<select id="city">

<option value="guangzhou">广州</option>

<option value="beijing">北京</option>

<option value="shanghai">上海</option>

<option value="shenzhen">深圳</option>

</select>

<span id="result">na</span>

<script>

var div1 = document.getElementById("city");

// window.onload = function(){alert(div1.value);};

var XHR = function() {

if( typeof XMLHttpRequest == "undefined") {

var xhrNames = ["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP","Microsoft.XMLHTTP"];

for (var i = 0; i < xhrNames.length; i++) {

try {

var XHR = new ActiveXObject(xhrNames[i]);

break;

} catch(e) {}

}

if (typeof XHR != undefined)

return XHR;

else

new Error("Ajax not supported!");

} else {

return new XMLHttpRequest();

}

}


var myXhr = XHR();

myXhr.onreadystatechange = handleStatusChange;

myXhr.open("get","db.asp?city=" + div1.value,true);


myXhr.send("");

function handleStatusChange() {

if (myXhr.readyState == 4) {

// Transfer is finished!

var div2 = document.getElementById("result");

div2.innerHTML = myXhr.responseText;

}

}


function sendRequest()

{

myXhr.open("get",true);

myXhr.send("");

}


if("\v"=="v") {

div1.onpropertychange = sendRequest;

}

else

div1.addEventListener("change",sendRequest);


</script>

</body>

</html>

原文链接:https://www.f2er.com/ajax/163640.html

猜你在找的Ajax相关文章