AJAX - 返回响应文本

发布于 2024-12-28 23:54:34 字数 1317 浏览 0 评论 0原文

我在互联网上看到了无数关于 AJAX 请求中返回未定义的类似代码的线程:

AJAX.onreadystatechange = function() {
        if(AJAX.readyState == 4) {
            if(AJAX.status == 200) {
                var response = AJAX.responseText;
                return response;
            }
            else {
                window.alert('Error: ' + AJAX.status);
                return false;
            }
        }
};

我知道我应该“用”responseText“做一些事情”,比如将其写入 HTML 。问题是:我没有那种奢侈。这段代码旨在用于运行快速 AJAX 请求的通用方法中,这样发出 AJAX 请求的所有代码都不必一遍又一遍地写出(~40×),并且有可能出现轻微的错误。这里或那里的问题会破坏应用程序。

我的方法必须显式返回responseText“否则”。 不写入 HTML。我该怎么做?另外,我希望缺少 JQuery 插件。

我正在寻找什么:

function doAjax(param) {
    // set up XmlHttpRequest
    AJAX.onreadystatechange = function() {
            if(AJAX.readyState == 4) {
                if(AJAX.status == 200) {
                    var response = AJAX.responseText;
                    return response;
                }
                else {
                    window.alert('Error: ' + AJAX.status);
                    return false;
                }
            }
    };
    // send data
}
...
function doSpecificAjax() {
    var param = array();
    var result = doAjax(param);
    // manipulate result
}

I've seen the myriad threads sprawled across the Internet about the following similar code in an AJAX request returning undefined:

AJAX.onreadystatechange = function() {
        if(AJAX.readyState == 4) {
            if(AJAX.status == 200) {
                var response = AJAX.responseText;
                return response;
            }
            else {
                window.alert('Error: ' + AJAX.status);
                return false;
            }
        }
};

I know that I'm supposed to "do something with" responseText like writing it to the HTML. The problem: I don't have that luxury. This bit of code is intended to be inside of a generic method for running fast AJAX requests that way all the code for making an AJAX request doesn't have to written out over and over again (~40×) with the chance of a minor problem here or there that breaks the application.

My method HAS to explicitly return responseText "or else." No writing to HTML. How would I do this? Also, I'd appreciate a lack of plugs for JQuery.

What I'm looking for:

function doAjax(param) {
    // set up XmlHttpRequest
    AJAX.onreadystatechange = function() {
            if(AJAX.readyState == 4) {
                if(AJAX.status == 200) {
                    var response = AJAX.responseText;
                    return response;
                }
                else {
                    window.alert('Error: ' + AJAX.status);
                    return false;
                }
            }
    };
    // send data
}
...
function doSpecificAjax() {
    var param = array();
    var result = doAjax(param);
    // manipulate result
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被你宠の有点坏 2025-01-04 23:54:34

做了一些研究,我发现了这篇 SOF 帖子:
Ajax responseText 返回未定义

根据该帖子,您可能会想要像这样实现你的ajax方法:

function doRequest(url, callback) {
    var xmlhttp = ....; // create a new request here

    xmlhttp.open("GET", url, true); // for async
    xmlhttp.onreadystatechange=function() {     
        if (xmlhttp.readyState==4) {
            if (xmlhttp.status == 200) {

                // pass the response to the callback function
                callback(null, xmlhttp.responseText);

            } else {
                // pass the error to the callback function
                callback(xmlhttp.statusText);
            }
        }
    }
    xmlhttp.send(null);
}

然后,你可以像这样调用该方法...

doRequest('http://mysite.com/foo', function(err, response) { // pass an anonymous function
    if (err) {
        return "";

    } else {
        return response;
    } 
});

这应该准确地返回responseText。如果这不能给您返回正确的结果,请告诉我。

Doing a little research I came across this SOF post:
Ajax responseText comes back as undefined

Based on that post, it looks like you may want to implement your ajax method like this:

function doRequest(url, callback) {
    var xmlhttp = ....; // create a new request here

    xmlhttp.open("GET", url, true); // for async
    xmlhttp.onreadystatechange=function() {     
        if (xmlhttp.readyState==4) {
            if (xmlhttp.status == 200) {

                // pass the response to the callback function
                callback(null, xmlhttp.responseText);

            } else {
                // pass the error to the callback function
                callback(xmlhttp.statusText);
            }
        }
    }
    xmlhttp.send(null);
}

Then, you can call that method like this...

doRequest('http://mysite.com/foo', function(err, response) { // pass an anonymous function
    if (err) {
        return "";

    } else {
        return response;
    } 
});

This should return the responseText accurately. Let me know if this doesn't give you back the correct results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文