Ajax+ASP和Flash+ASP數(shù)據(jù)存取方法
兩種數(shù)據(jù)存取方法差不多。
===============================
下面是一個(gè)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("不能創(chuàng)建XMLHttpRequest對(duì)象實(shí)例");
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("請(qǐng)輸入內(nèi)容");
}
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("您請(qǐng)求的頁(yè)面有異常");
//alert("您請(qǐng)求的頁(yè)面有異常");
}
}
}
function getAllMsg() {
setSuggest("nbsp;");
ajaxSendPost("process.asp","",processSendRequest);
if(sound) {
setSuggest("embed type=\"application/x-mplayer2\" src=\"sound/message.wav\"
autostart=\"true\" loop=\"false\" height=0 width=0 />nbsp;");
sound=false;
}
}
function IamComing() {
ajaxSendPost("iamcoming.asp", "", processSendRequest);
(document.getElementById("msg")).focus();
}
function showOnline() {
ajaxSendPost("showOnline.asp", "", processShowOnline);
}
function processShowOnline() {
if(ajaxHttpRequest.readyState==4) {
if(ajaxHttpRequest.status==200) {
if(isFinite(ajaxHttpRequest.responseText)) {
document.getElementById("online").innerHTML =
ajaxHttpRequest.responseText;
}
}
}
}
=================================
下面是我一個(gè)Flash留言的數(shù)據(jù)讀取的部分代碼: http://www.linjimu.com.cn/Flash
ls = new LoadVars();
ls.Action = "Read";
ls.CurrentPage = _root.CurrentPage;
//ls load and send ,ld load result;
ld = new LoadVars();
ls.sendAndLoad("Advice.asp", ld, "post");
_root.gotoAndPlay("Wait");
_root.WaitBtText = "返回留言";
_root.Frame = "Send";
_root.TextMessage.text = "\n 正在讀取留言數(shù)據(jù)...\n\n 請(qǐng)稍后...";
ld.onLoad = function(ok) {
if (ok) {
if (this.message == "OK") {
_root.gotoAndPlay("ListView");
} else {
_root.gotoAndPlay("Wait");
_root.WaitBtText = "返回留言";
_root.Frame = "Send";
_root.TextMessage.text = " 讀取數(shù)據(jù)不成功!\n\n 可能發(fā)生以下錯(cuò)誤:\n 1.
讀取數(shù)據(jù)超時(shí),請(qǐng)稍后再試.\n 2.空間不支持ASP."+this.message;
}
} else {
_root.gotoAndPlay("Wait");
_root.WaitBtText = "返回留言";
_root.Frame = "Send";
_root.TextMessage.text = " 讀取數(shù)據(jù)不成功!\n\n 可能發(fā)生以下錯(cuò)誤:\n 1.讀取數(shù)據(jù)
超時(shí),請(qǐng)稍后再試.\n 2.空間不支持ASP.";
}
};
delete ls;
stop();
================
相比一下,他們都有相似之處:
AJax:
ajaxHttpRequest.open("POST",url,true);//發(fā)送數(shù)據(jù)的方法,類型,url地址..
ajaxHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajaxHttpRequest.send(values);//發(fā)送數(shù)據(jù)
ajaxHttpRequest.onreadystatechange = processRequest; //processRequest是一個(gè)過程函數(shù),對(duì)返回?cái)?shù)據(jù)的
處理。
--------
Flash:
ls = new LoadVars();
ls.Action = "Read";//是發(fā)送數(shù)據(jù)
ls.CurrentPage = _root.CurrentPage;//是發(fā)送數(shù)據(jù)
//ls load and send ,ld load result;
ld = new LoadVars();
ls.sendAndLoad("Advice.asp", ld, "post");//發(fā)送數(shù)據(jù)的方法,類型,url地址..
ld.onLoad = function(ok) {//code...} //也是一個(gè)過程函數(shù),對(duì)返回?cái)?shù)據(jù)的處理。
不過,在web方面,Ajax的頁(yè)面完全基于HTML,文本網(wǎng)頁(yè)會(huì)更有利于搜索引擎的搜索。
Flash開發(fā)人員還是偏重圖形、動(dòng)畫設(shè)計(jì),F(xiàn)lash能夠更容易的調(diào)用瀏覽器以外的外部資源。比如攝像頭、麥克風(fēng)等。然而這是普通的HTML無法完成的。
他們的關(guān)系請(qǐng)去baidu一下:flash與AJAX http://www.baidu.com/s?wd=flash+ajax