ajax hello world

原创
2015/03/23 17:09
阅读数 156

两个文件,一个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", "db.asp?city=" + div1.value, true);

myXhr.send("");

}


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

div1.onpropertychange = sendRequest;

}

else

div1.addEventListener("change", sendRequest);


</script>

</body>

</html>


展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部