?? ajax_func.js
字號:
/**
* @author star
* 描述:這是使用ajax的模版函數,只需要按指定的參數傳入值就可以了。
*/
//定義http_request對象實列
var http_request = false;
//定義可復用的http請求發送函數
function send_request(method,url,content,responseType,callBack)
{ //初始化,指定處理函數,發送請求的函數。
http_request =false;
//開始初始化XMLHttpRequest對象
if(window.XMLHttpRequest)
{
//Mozilla瀏覽器
http_request = new XMLHttpRequest();
if(http_request.overrideMimeType)
{
//設置Mime類別
http_request.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject)
{
try
{ //在IE7.0之前使用
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e) {}
}
}
if(!http_request)
{
//異常,創建對象實例失敗
window.alert("不能創建XMLHttpRequest對象");
return false;
}
//指定處理函數
if(responseType.toLowerCase()=="text")
{
http_request.onreadystatechange=callBack;
}
else if(responseType.toLowerCase()=="xml")
{
http_request.onreadystatechange=callBack;
}
else
{
window.alert("響應類別參數錯誤。");
return false;
}
//確定發送請求的方式和url以及是否異步執行下段代碼
if(method.toLowerCase()=="get")
{
http_request.open(method,url,true);
}
else if(method.toLowerCase =="post")
{
http_request.open(method,url,true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
else
{
window.alert("http請求類別參數錯誤。");
return false;
}
//發送請求
http_request.send(content);
}
//處理返回文本格式信息的函數
function processTextResponse()
{
if(http_request.readyState ==4) //判斷對象狀態
{
if(http_request.status == 200) //信息已成功返回。開始處理信息
{
alert("文本文件響應");
document.getElementById("output").innerHTML = "Time if for" + http_request.responseText;
}
else //頁面不正常
{
alert("您請求的頁面有異常。");
}
}
}
//處理返回xml格式信息的函數
function processXMLResponse()
{
if(http_request.readyState ==4) //判斷對象狀態
{
if(http_request.status == 200) //信息已成功返回。開始處理信息
{
alert("xml文檔響應");
}
else //頁面不正常
{
alert("您請求的頁面有異常。");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -