当前位置:多学网学习教育电脑学习编程入门JAVA教程最小的AJAX类库 microajax

最小的AJAX类库 microajax

[08-23 22:11:04]   来源:http://www.duoxue8.com  JAVA教程   阅读:574
最小的AJAX类库 microajax,标签:JAVA编程技巧,java培训,java视频教程,http://www.duoxue8.com

最小的AJAX类库
One of the smallest and easiest AJAX libraries

使用方法:

microAjax("/resource/url"function (res) {
  alert (res);
});

服务端:

if ($_SERVER['X-Requested-With'] == 'XMLHttpRequest') {
  // do something clever
}

源代码:

// microAjax by Stefan Lange-Hegermann
// this code is in the public domain
// you can do with it whatever you want!
 
function microAjax(urlcallbackFunction)
{
    this.bindFunction = function (callerobject) {
        return function() {
            return caller.apply(objectnew Array(object));
        }
    }
 
    this.stateChange = function (object) {
        if (this.request.readyState==4) {
            this.callbackFunction(this.request.responseText);
        }
    }
 
    this.getRequest = function() {
        if (window.ActiveXObject)
            return new ActiveXObject('Microsoft.XMLHTTP');
        else if (window.XMLHttpRequest)
            return new XMLHttpRequest();
        else
            return false;
    }
 
    if (arguments[2])
        this.postBody = arguments[2];
    else 
        this.postBody="";
 
    this.callbackFunction=callbackFunction;
    this.url=url;   
    this.request = this.getRequest();
 
    if(this.request) {
        this.request.onreadystatechange = this.bindFunction(this.stateChangethis);
 
        if (this.postBody!="") {
            this.request.open("POST"urltrue);
            this.request.setRequestHeader('Content-type''application/x-www-form-urlencoded');
            this.request.setRequestHeader('Connection''close');
        } else {
            this.request.open("GET"urltrue);
        }
 
        this.request.send(this.postBody);
    }
}

压缩版源代码:

function microAjax(B,A){this.bindFunction=function(E,D){return function(){return E.apply(D,[D])}};this.stateChange=function(D){if(this.request.readyState==4){this.callbackFunction(this.request.responseText)}};this.getRequest=function(){if(window.ActiveXObject){return new ActiveXObject("Microsoft.XMLHTTP")}else{if(window.XMLHttpRequest){return new XMLHttpRequest()}}return false};this.postBody=(arguments[2]||"");this.callbackFunction=A;this.url=B;this.request=this.getRequest();if(this.request){var C=this.request;C.onreadystatechange=this.bindFunction(this.stateChange,this);if(this.postBody!==""){C.open("POST",B,true);C.setRequestHeader("X-Requested-With","XMLHttpRequest");C.setRequestHeader("Content-type","application/x-www-form-urlencoded");C.setRequestHeader("Connection","close")}else{C.open("GET",B,true)}C.send(this.postBody)}};
本文相关附件下载: 下载统计:次数:21  人气:218
  下载地址 microajax.zip 0.7(KB)

最小的AJAX类库 microajax 结束。
Tag:JAVA教程JAVA编程技巧,java培训,java视频教程电脑学习 - 编程入门 - JAVA教程
最小的AJAX类库 microajax相关文章