ajax hello world

兩個文件,一個db.asp,一個weather.html
html

db.asp 就是返回溫度值spa

<%htm

   dim cityip

   city = Request.QueryString("city")ci

   if city = "guangzhou" thenget

      Response.Write("31")it

   elseif(city = "beijing") thenio

      Response.Write("21")      function

   elseList

      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>

相關文章
相關標籤/搜索