www.gusucode.com > 漂亮的地方旅游景点景观介绍网站源代码 > inc/ajaxrequest.js

    function oAjax( url,callback)
{
try{
this.HttpRequest = null;
this.Debug= false;
this.Url = url;
this.ContentType = "text/xml";
this.HttpRequest = this.createXMLHttpRequest();

if ( this.HttpRequest == null )
{
this._debug("XMLHttpRequest create failure!");
return;
}

var xhReq = this.HttpRequest;
xhReq.onreadystatechange = function (){
oAjax._OnReadyStateChange( xhReq,callback );
}

} catch(e){
this._debug( "unknow err: " + e.message );
}
}

/*
 * Get URL resource
 */
oAjax.prototype.Get = function() {

this.SetContentType( "text/html" );
this._get();
}

/*
 * Post data to the server
 */
oAjax.prototype.Post = function( arrKey, arrValue ) {

var data = '';
this.SetContentType( "application/x-www-form-urlencoded" );
for( i = 0; i < arrKey.length; i ++)
{
data += "&" + escape(arrKey[i]) + "=" + escape(arrValue[i]);
//data += "&" + arrKey[i] + "=" + arrValue[i];
}
//document.write(data);
data = data.replace(/^&/g, "");
this._post(data);
}

/*
 * Initialization for oAjax class
 */
oAjax.prototype.Init = function() {
// initialization
}

/*
 * Change URL for request
 */
oAjax.prototype.SetUrl = function( url ) {
this.Url = url;
}

/*
 * Set content type for HTTP header before sending request
 */
oAjax.prototype.SetContentType = function( type ) {
this.ContentType = type;
}

oAjax.prototype.createXMLHttpRequest = function() {

try { return new ActiveXObject("Msxml2.XMLHTTP");} catch(e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
try { return new XMLHttpRequest(); } catch(e) {}
return null;
}

/*
 * Debug information for testing
 */
oAjax.prototype._debug = function(message) {

if ( this.Debug )
{
alert(message);
}
}

/*
 * Process message and data from server
 */
oAjax._OnReadyStateChange = function( xreq, callback ){

if ( xreq == null )
{
return;}

/*Status is completed, then process result */
if ( xreq.readyState == 4)
{
// OK
if ( xreq.status == 200 )
{
//alert(xreq.responseText);
callback (xreq.responseText); 
}else{
//alert('服务器端错误!');
document.write (xreq.responseText);
}
} else {
// Others
}
}

oAjax.prototype._SendRequest = function(HttpMethod, data){

this._debug( 'Send Request ' + HttpMethod + data );

if ( this.HttpRequest != null )
{
this.HttpRequest.open(HttpMethod, this.Url, true);

if ( this.ContentType != null )
{
//<FORM> MIME type: application/x-www-form-urlencoded
this.HttpRequest.setRequestHeader("Content-Type", this.ContentType);
}
this.HttpRequest.send(data);
return true;
}
return false;
}

/* Send GET request to server */
oAjax.prototype._get = function () {

this._debug( 'GET' );
return this._SendRequest("GET", null);
}

/* Send POST request and data to server */
oAjax.prototype._post = function (data) {

this._debug( 'POST' );
return this._SendRequest("POST", data);
}

oAjax.ArrayValue = function ( xmlobj ) {
var array = new Array();
var i = 0;
var response = xmlobj.getElementsByTagName('Response')[0];
var element = response.firstChild;
array[i] = element.firstChild.nodeValue;
while ( element = element.nextSibling )
{
i ++;
array[i] = element.firstChild.nodeValue;
}
return array;
}

//表单验证////////////////////////////////////////////////////////////////////////////
//email
function isEmail(strEmail) {
if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1){
return true;
}
else
{
return false;	
}
}
//正确
function ok(obj,str)
{
GE(obj).className="ok";
GE(obj).innerHTML="<img src=\"/images/icon/ok.gif\"> "+ str
}
//错误
function err(obj,str)
{
GE(obj).className="err";
GE(obj).innerHTML="<img src=\"/images/icon/err.gif\"> "+ str
}
//loading
function loading(obj)
{
GE(obj).className="load";
GE(obj).innerHTML="<img src=\"/images/icon/loading.gif\"> 正在建立连接..."
}

function ls(str) {
var totallength=0;for (var i=0;i<str.length;i++)
{
var intCode=str.charCodeAt(i); if (intCode>=0&&intCode<=128) {
totallength=totallength+1; //非中文单个字符长度加 1
}
else {
totallength=totallength+2; //中文字符长度则加 2
}
} //end for
return totallength;
}
//正则
function pn(pt,str)
{
var patrn=pt;
if (!patrn.exec(str)) return false
return true
}