两个封装的比较好的AJAX函数
2009-06-02 15:45 Jeffery Tao 阅读(334) 评论(0) 收藏 举报1、******************************************************
/// <summary>
/// 调用ajax,返回服务器端的原始数据
/// </summary>
/// <param name="url">远程调用路径</param>
/// <param name="pars">附加到路径的url参数</param>
/// <param name="method">请求方式,get或post</param>
/// <param name="onComplete">此变量是一个函数,数据请求成功后要调用的函数</param>
/// <param name="asynchronous">是否异步调用,true和false</param>
/// <param name="cachecontrol">是否保存cache,true和false</param>
///此类由frankie封装,请不要删除此信息
var xmlHttp = null;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function sendAJAX(url, pars, method, onComplete, asynchronous, cachecontrol) {
createXMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
//此处可对应更改为xmlHttp.responseXML和xmlHttp.responseStream
onComplete(xmlHttp.responseText);
}
else {
alert("there is an error in server!the status is:" + xmlHttp.status.toString());
}
}
}
if (method.toLowerCase() == "get") {
if (!cachecontrol) {
if (pars != "") {
pars += "&time=" + new Date().toString();
}
else {
pars += "?time=" + new Date().toString();
}
}
if (pars != "") {
url += "?" + pars;
}
xmlHttp.open("GET", url, asynchronous);
xmlHttp.send(null);
}
else {
if (!cachecontrol) {
url += "?time=" + new Date().toString();
}
xmlHttp.open("POST", url, asynchronous);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");//必须加,否则后台取不到数据
xmlHttp.send(pars);
}
}
调用方式:
引用该js文件后直接使用
sendAJAX("AjaxTest.ashx", "id=1", "post", onComplete, true, false);
即可,onComplete是处理后台取来的数据函数
**********************************************************
/// <summary>
/// 调用ajax,返回服务器端的原始数据
/// </summary>
/// <param name="url">远程调用路径</param>
/// <param name="pars">附加到路径的url参数</param>
/// <param name="method">请求方式,get或post</param>
/// <param name="onComplete">此变量是一个函数,数据请求成功后要调用的函数</param>
/// <param name="asynchronous">是否异步调用,true和false</param>
//此类由frankie封装,请不要删除此信息
var xmlHttp=null;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
CreateIEXMLHTTPRequest();
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
// 尽量得到版本较高的XMLHTTP对象
function CreateIEXMLHTTPRequest()
{
if(window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
var xmlHttps = ["Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.3.0",
"Msxml2.XMLHTTP",
"Microsoft.XMLHTTP"];
for(var i = 0; i < xmlHttps.length; i++ )
{
try
{
return new ActiveXObject(xmlHttps[i]);
}
catch(e){
return false;
}
}
return null;
}
function sendAJAX(url,params,method,onComplete,asynchronous)
{
createXMLHttpRequest();
//默认用get提交
if(method.toLowerCase() == "get")
{
url = url+"?"+params+"&time=" +new Date().getTime();
xmlHttp.open("GET",url,asynchronous);
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
//if(xmlHttp.status==200)
//{
//此处可对应更改为xmlHttp.responseXML和xmlHttp.responseStream
onComplete(xmlHttp.responseText);
//}
}
}
xmlHttp.send(null);
}
else
{
xmlHttp.open("POST",url,asynchronous);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(params);
}
}
function sendSms(params,method,onComplete,asynchronous)
{
createXMLHttpRequest();
//默认用get提交
var url="http://www.boolu.com/CMSInterFace/CMSSendMsg.asmx/SendMsg";
if(method.toLowerCase() == "get")
{
url = url+"?"+params+"&SendTime=" +getDateTime();
xmlHttp.open("GET",url,asynchronous);
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
//if(xmlHttp.status==200)
//{
//此处可对应更改为xmlHttp.responseXML和xmlHttp.responseStream
var str=xmlHttp.responseXML.getElementsByTagName("string").item(0).firstChild.data;
onComplete(str);
//}
}
}
xmlHttp.send(null);
}
else
{
xmlHttp.open("POST",url,asynchronous);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(params);
}
}
function getDateTime()
{
var now= new Date();
var year=now.getYear();
var month=now.getMonth()+1;
var day=now.getDate();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second
}
浙公网安备 33010602011771号