📄 joajax.js
字号:
/*************************************************************************
* Copyright By Jiny Wu
*
* JoAjax.js
*
* This is a base class for AJAX
*
* var Ajax = new JoAjax(Url, Xml, Callback);
*
* Url: Request url
* Xml: Determine whether it uses responseXML or responseText
* Callback: User-defined callback function to process result
* Declaration: void Callback( result )
* If responseXml then result = XmlDom object
* If responseText then result = HTML text
*
* Interface declaration
* 1) Call( string MethodName, ...): Call server method (POST request)
* Call method via JoAjax
* Ajax->Call( methodName, param1, param2, ...);
*
* 2) Get(): Send GET request
* Ajax->Get();
*
* 3) Post( array key, array value ): Post form key/value
* Ajax->Post( arrKey, arrValue );
*
* Utility functions declaration
* 1) HtmlEntity(string HtmlText): convert html entity
* 2) StripStags(string HtmlText): remove html tags
*
* Author: Jiny Wu
* Email: wjyong@sh163.net
* Date: 2005/12/02
* Version: 1.0
*
* Notice: ANYONE CAN DUPLICATE THE CLASS FOR STUDY AND ANY NON-BUSINESS PURPOSE,
* BUT PLEASE DON'T USE IT AS ANY BUSINESS PURPOSE. IF YOU REALLY WANT TO
* DO THAT, PLEASE CONTACT ME AND BE GRANTED. IF THERE IS ANY BUG, PLEASE
* REPORT IT TO ME AS SOON AS POSSIBLE. PLEASE DON'T REMOVE THE NOTICE AND
* COPYRIGHT AFTER DUPLICATING. ANY OPINION WILL BE WELCOME ALSO.
*
* $Id 2005/12/02 8:00:00 $
*************************************************************************/
/* Error constants definition */
var ERR_OK = 0;
var ERR_UNKNOWN_EXCEPTION = 1;
var ERR_SERVER_SCRIPT = 2;
var ERR_XMLHTTPREQUEST_FAILURE = 3;
var ERR_XML_NOTFORMED = 4;
var ERR_CALLBACK_FAIL = 5;
/*
* JoAjax class definition
* It's a constructor of JoAjax class
*/
function JoAjax( url , xml, callback)
{
try{
this.HttpRequest = null;
this.Debug = false;
this.Url = url;
this.ContentType = "text/xml";
this.CallbackFunction = callback;
this.bXml = xml;
this.Error = ERR_OK;
this.HttpRequest = this.createXMLHttpRequest();
if ( this.HttpRequest == null )
{
this._debug("XMLHttpRequest create failure!");
this.Error = ERR_XMLHTTPREQUEST_FAILURE;
return;
}
var xhReq = this.HttpRequest;
xhReq.onreadystatechange = function (){
JoAjax._OnReadyStateChange( xhReq, xml, callback );
}
} catch(e){
this.Error = ERR_UNKNOWN_EXCEPTION;
this._debug( "JoAjax: " + e.message );
}
}
/*
* Get URL resource
*/
JoAjax.prototype.Get = function() {
this.SetContentType( "text/html" );
this._get();
}
/*
* Post data to the server
*/
JoAjax.prototype.Post = function( arrKey, arrValue ) {
var data = '';
this.SetContentType( "application/x-www-form-urlencoded" );
for( i = 0; i < arrKey.length; i ++)
{
data += "&" + encodeURIComponent(arrKey[i]) + "=" + encodeURIComponent(arrValue[i]);
}
data = data.replace(/^&/g, "");
this._post(data);
}
/*
* XMLRPC call functions in the server side.
* Arguments are arbitrary, at least one,which show the name of caller function
*/
JoAjax.prototype.Call = function() {
var data = '';
var i = 0;
this.SetContentType( "text/xml" );
this._assert( arguments.length >= 1, "There is no specified function name");
data = "<Request>\n<methodName>"+ arguments[0] +"</methodName>\n";
for ( i = 1; i < arguments.length; i ++)
{
data = data + "<param" + i + ">"+ arguments[i] + "</param"+ i +">\n";
}
data = data + "</Request>";
this._post(data);
}
/*
* Extract scalar value from xml object
* <Response>
* scalarvalue <=> scalarvalue
* </Response>
*/
JoAjax.ScalarValue = function ( xmlobj ) {
if ( xmlobj )
{
try
{
return xmlobj.getElementsByTagName('Response')[0].firstChild.nodeValue;
}
catch (e)
{
return null;
}
}
return null;
}
/*
* Extract array value from xml object
* <Response>
* <item>1</item>
* <item>2</item> <=> array(1, 2);
* </Response>
*/
JoAjax.ArrayValue = function ( xmlobj ) {
var array = new Array();
if ( xmlobj )
{
try
{
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;
}
catch (e)
{
return array;
}
}
return array;
}
/*
* Initialization for JoAjax class
*/
JoAjax.prototype.Init = function() {
// initialization
}
/*
* Change URL for request
*/
JoAjax.prototype.SetUrl = function( url ) {
this.Url = url;
}
/*
* Set content type for HTTP header before sending request
*/
JoAjax.prototype.SetContentType = function( type ) {
this.ContentType = type;
}
JoAjax.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
*/
JoAjax.prototype._debug = function(message) {
if ( this.Debug )
{
alert(message);
}
}
/*
* Process message and data from server
*/
JoAjax._OnReadyStateChange = function( xreq, xml, callback ){
if ( xreq == null )
{
return;
}
/*Status is completed, then process result */
if ( xreq.readyState == 4)
{
// OK
if ( xreq.status == 200 )
{
if ( xml )
{
var responseXML = xreq.responseXML;
try
{
if ( responseXML.xmlText == "" )
{
alert('Server Script Error!' + "\n" + JoAjax.StripTags(xreq.responseText) );
this.Error = ERR_SERVER_SCRIPT;
} else {
try
{
callback ( responseXML );
}
catch (e)
{
this.Error = ERR_CALLBACK_FAIL;
alert( "Callback " + e.name + " : " + e.message + "\n" + callback);
}
}
}
catch (e)
{
alert('Xml not well-formed : ' + responseXML.xmlText );
this.Error = ERR_XML_NOTFORMED;
return;
}
} else {
try
{
callback ( xreq.responseText );
}
catch (e)
{
this.Error = ERR_CALLBACK_FAIL;
alert( "Callback " + e.name + " : " + e.message + "\n" + callback);
}
}
}
} else {
// Others
}
}
/*
* Send HTTP request to the server
* Params:
* HttpMethod: HTTP protocol specified method: POST, GET, PUT, HEAD, etc.
* data: HTTP entity data. it can be null if the method is GET, PUT and HEAD
* Return:
* true if successfully, or false
*/
JoAjax.prototype._SendRequest = function(HttpMethod, data){
this._debug( 'Send Request ' + HttpMethod + data );
this._assert ( this.HttpRequest != null , "XMLHttpRequest should not be NULL");
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 */
JoAjax.prototype._get = function () {
this._debug( 'GET' );
return this._SendRequest("GET", null);
}
/* Send POST request and data to server */
JoAjax.prototype._post = function (data) {
this._debug( 'POST' );
return this._SendRequest("POST", data);
}
/*
* ASSERT statement for program robust
* It"s used to check arguments of functions
*/
JoAjax.prototype._assert = function(condition, errmsg) {
if ( !condition )
{
this._debug("JoAjax ASSERT(): " + errmsg);
}
}
/*
* Convert html entity to string
*
* Example:
*
* "<item>" => "<item>"
*/
JoAjax.HtmlEntity = function ( html ) {
html = html.replace(/&/g, "&");
html = html.replace(/</g, '<');
html = html.replace(/>/g, '>');
return html;
}
/*
* Remove HTML tags
*
* Example:
*
* "<font><b>example</b> hello, world!</font>" => "example hello, world!"
*/
JoAjax.StripTags = function ( html ){
html = html.replace(/<[^>]+>/g, "");
return html;
}
function gb2utf8(data){
var glbEncode = [];
gb2utf8_data = data;
execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");
var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");
t=t.split("@");
var i=0,j=t.length,k;
while(++i<j) {
k=t[i].substring(0,4);
if(!glbEncode[k]) {
gb2utf8_char = eval("0x"+k);
execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript");
glbEncode[k]=escape(gb2utf8_char).substring(1,6);
}
t[i]=glbEncode[k]+t[i].substring(4);
}
gb2utf8_data = gb2utf8_char = null;
return unescape(t.join("%"));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -