簡體   English   中英

Javascript函數返回undefined而不是object

[英]Javascript function returns undefined instead of object

我是一個試圖處理JavaScript的Perl編碼器。 嘗試發出AJAX請求,但函數始終返回undefined 此代碼有什么問題? 如何使其正常工作?

var url = 'http://local.com/cgi-bin/hello2.pl';
var params = 'a=b&c=d';
// url returns a plain text:
// 1234567890 2013 05 May Friday 13 23 45 01

var enddate = getEndDate(url, params);
var dow = enddate.EDDAYOW; // must be a 'Friday' but returns undefined
alert(dow);



function getEndDate(url, params) {
    var myRequest = new ajaxObject(url);
    myRequest.callback = function(responseText) {
        if (responseText.length > 20) {
            var n = responseText.split(" ");
            return {
                'edseconds': n[0],
                'EDYEAR': n[1],
                'EDMON': n[2],
                'EDMONNAME': n[3],
                'EDDAYOW': n[4],
                'EDDAY': n[5],
                'EDHOUR': n[6],
                'EDMIN': n[7],
                'EDSEC': n[8]
            };
        } else {
            getEndDate(url, params);
        }
    }
    myRequest.update(params);
}



function ajaxObject(url, callbackFunction) {
    var that = this;
    this.updating = false;
    this.abort = function() {
        if (that.updating) {
            that.updating = false;
            that.AJAX.abort();
            that.AJAX = null;
        }
    }
    this.update = function(passData, postMethod) {
        if (that.updating) {
            return false;
        }
        that.AJAX = null;
        if (window.XMLHttpRequest) {
            that.AJAX = new XMLHttpRequest();
        } else {
            that.AJAX = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (that.AJAX == null) {
            return false;
        } else {
            that.AJAX.onreadystatechange = function() {
                if (that.AJAX.readyState == 4) {
                    that.updating = false;
                    that.callback(that.AJAX.responseText, that.AJAX.status, that.AJAX.responseXML);
                    that.AJAX = null;
                }
            }
            that.updating = new Date();
            if (/post/i.test(postMethod)) {
                var uri = urlCall + '?' + that.updating.getTime();
                that.AJAX.open("POST", uri, true);
                that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                that.AJAX.setRequestHeader("Content-Length", passData.length);
                that.AJAX.send(passData);
            } else {
                var uri = urlCall + '?' + passData + '&timestamp=' + (that.updating.getTime());
                that.AJAX.open("GET", uri, true);
                that.AJAX.send(null);
            }
            return true;
        }
    }
    var urlCall = url;
    this.callback = callbackFunction ||
    function() {};
}

除了上面提到的異步內容和返回內容之外,我還假設您正在嘗試將某些內容與服務器時間戳進行同步。 您只需要使用millis並使用javascript date對象即可完成其余工作。

如果服務器沒有快速響應或回答> 20個字符,這也是一個無限循環,可能會在堆棧溢出一段時間后導致瀏覽器崩潰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM