Ajax+ASP和Flash+ASP数据存取方法
两种数据存取方法差不多。
===============================
下面是一个ChatRoom的Ajax部分代码:
var ajaxHttpRequest = false;
function ajaxInit() {
if(window.XMLHttpRequest) { //Mozilla, Opera, ...
ajaxHttpRequest = new XMLHttpRequest();
if(ajaxHttpRequest.overrideMimeType) {
ajaxHttpRequest.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject) { //IE
try{
ajaxHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try{
ajaxHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
}
}
}
if(!ajaxHttpRequest) {
window.alert("不能创建XMLHttpRequest对象实例");
return false;
}
}
function ajaxSendPost(url, values, processRequest) {
ajaxHttpRequest.open("POST",url,true);
ajaxHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajaxHttpRequest.send(values);
ajaxHttpRequest.onreadystatechange = processRequest;
}
/*
function ajaxSendGet(url) {
ajaxHttpRequest.open("GET",url,true);
ajaxHttpRequest.send(null);
ajaxHttpRequest.onreadystatechange = processRequest;
}
*/
ajaxInit();
var sound = false;
var isMove = true;
function send() {
var msg=escape((document.getElementById("msg")).value); //escape解决Ajax中文筹码问题
if(msg=="") {
setSuggest("请输入内容");
}
else {
var color = document.getElementById("selectColor").value;
var values = "msg=" + msg + "&color=" + color;
ajaxSendPost("process.asp", values, processSendRequest);
document.getElementById("msg").value = "";
document.getElementById("msg").focus();
}
}
function processSendRequest() {
if(ajaxHttpRequest.readyState==4) {
if(ajaxHttpRequest.status==200) {
if(ajaxHttpRequest.responseText!="") {
var chatContent = document.getElementById("chat_content");
var msgDiv = document.createElement("div");
msgDiv.innerHTML = ajaxHttpRequest.responseText;
chatContent.appendChild(msgDiv);
sound = true;
}
}
else {
setSuggest("您请求的页面有异常");
//alert("您请求的页面有异常");
}
}
}
function getAllMsg() {
setSuggest(" ");
ajaxSendPost("process.asp","",processSendRequest);
if(sound) {
setSuggest("