简体   繁体   English

Javascript函数返回undefined而不是object

[英]Javascript function returns undefined instead of object

I am a Perl coder trying to deal with a JavaScript. 我是一个试图处理JavaScript的Perl编码器。 Trying to make an AJAX request but function always returns undefined . 尝试发出AJAX请求,但函数始终返回undefined What is wrong with this code? 此代码有什么问题? How to get it to work properly? 如何使其正常工作?

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() {};
}

Besides the async stuff AND the return stuff mentioned above I would assume that you're trying to sync something with a servers timestamp. 除了上面提到的异步内容和返回内容之外,我还假设您正在尝试将某些内容与服务器时间戳进行同步。 You only need the millis and use a javascript date object to do the rest. 您只需要使用millis并使用javascript date对象即可完成其余工作。

If the servers not responding fast or with some kind of answer > 20 chars, this is also an infinite loop causing a browser crash after some while at a stack overflow. 如果服务器没有快速响应或回答> 20个字符,这也是一个无限循环,可能会在堆栈溢出一段时间后导致浏览器崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM