1

这是我在 js 中的第一堂课,我需要帮助才能获得正确的结果

function ajaxObject (url, data, type) {
this.url = url;
this.data = 'data='+JSON.stringify(data) || null;
this.type = type ? type : 'json';
this.result = null;
var self = this;

this.create = function () {
    //se il browser ? firefox
    if (window.XMLHttpRequest) {
        //istanzio un nuovo oggetto XMLHttpRequest
        this.oHttp = new XMLHttpRequest();
        if (this.oHttp.overrideMimeType) {
            this.oHttp.overrideMimeType('text/xml');
        }
    //se il browser ? IE    
    } else if (window.ActiveXObject) {
        try {
            this.oHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                this.oHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
            }
        }
    }
};

this.process = function () {
    if (this.oHttp) {
        this.oHttp.open("POST", this.url, true);
        this.oHttp.onreadystatechange = this.manage;

        if (this.oHttp.overrideMimeType) {
            this.oHttp.overrideMimeType("application/json");
        }
        this.oHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        this.oHttp.setRequestHeader("Content-length", this.data.length);
        this.oHttp.send(this.data);
    }
};

this.manage = function () {
    // alert(self.oHttp.readyState + ' - ' + self.oHttp.responseText);
    if (this.readyState === 4 && this.status === 200) {
        // here this.responseText is correctly populated
        self.result = this.responseText;
        } else {
        self.requestFailed('message', this);
        return;
    }
};

this.getResult = function () {
    switch (this.type) {
        case 'text':
            return this.result;
            break;
        case 'json':
            return $.parseJSON(this.result);
            break;
    }
};

this.requestFailed = function (id, request) {
    if (document.getElementById) {
        sys.clearContent(id);
        document.getElementById(id).innerHTML = 'The XMLHttpRequest failed, STATUS: ' + request.status;
    }
};

}

我尝试以这种方式获得结果:

var ao = new ajaxObject('output.php', {mutator: "orders", get: "orders"});
ao.create();
ao.process();
var r = ao.getResult();
alert(r);

但我无法得到我期望的结果..所有服务器端都可以正常工作,但我的结果变量总是“null”..我想我想念一些东西,如果有人可以帮助我:)

4

0 回答 0