AJAX基本示例
<script>
//set the variables.
function getArticle(id) {
var link = "/getNews.asp";
xmlhttpPost(link, id);
}
//Make the actual connection.
function xmlhttpPost(strURL, id) {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) { //ready state 4 means its complete.
updatepage(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(getquerystring(id));
}
//set the query string to be sent
function getquerystring(id) {
qstr = 'data=' + escape(id); // NOTE: no '?' before querystring
return qstr;
}
//put the data on the page.
function updatepage(str) {
document.getElementById("data").innerHTML = str;
}
</script>
<select onchange="getArticle(this.value);">
<option value="1">Train hits car</option>
<option value="2">Airplane Crashes</option>
<option value="3">Mental Health</option>
</select>
<div id="data"></div>
现在获取getNews.asp ---不是全部功能的代码,而是一个示例
<%
news_id = request("id")
strSQL = "select * from news where news_id = " & new_id
rs.open strSQL, connection, 1, 1
if not rs.eof then
response.write rs("story")
end if
%>
From: https://bytes.com/topic/javascript/insights/666300-ajax-example