当我的 jqxhr.responseText 变量在 xhr 对象中设置时,为什么它未定义?

发布于 2024-12-28 02:22:41 字数 632 浏览 0 评论 0原文

我在这里有一个真正简单的 jquery get 调用,我想稍后在脚本中使用响应。因此,如果我这样做:

var xhr = $.get('cfc/partsDefinition.cfc',{
                method: 'checkValid',
                search: 'some search string'
            }
        );
        console.log(xhr);
        console.log(xhr.responseText);

我可以看到 A)第一个日志显示一个有效的 jqxhr 对象,responseText 属性设置为我期望的...

promise: function (a){if(a==null)a=h;else for(var b in h)a[b]=h[b];return a}
readyState: 4
responseText: "0"
setRequestHeader: function (a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this}

但是 B)第二个日志显示“未定义”。我在这里缺少什么?

I have a real simple jquery get call here, and I want to use the response later in the script. So if I do this:

var xhr = $.get('cfc/partsDefinition.cfc',{
                method: 'checkValid',
                search: 'some search string'
            }
        );
        console.log(xhr);
        console.log(xhr.responseText);

I can see that A) the first log shows a valid jqxhr object, with a responseText property set to what I expect...

promise: function (a){if(a==null)a=h;else for(var b in h)a[b]=h[b];return a}
readyState: 4
responseText: "0"
setRequestHeader: function (a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this}

but B) the second log shows "undefined". What am I missing here?

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

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

发布评论

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

评论(2

美煞众生 2025-01-04 02:22:42

它显示未定义,因为在代码中的该点,它是未定义的。 AJAX 是异步的,这意味着它不会在代码运行之前完成。在 ajax 调用完成之前,responseText 将处于未定义状态。这是一个例子:

var xhr = $.get('cfc/partsDefinition.cfc',{
                method: 'checkValid',
                search: 'some search string'
            }
        ).done(function(data){
            console.log("xhr",xhr);
            console.log("xhr.responseText",xhr.responseText);
            console.log("data",data);
        });
console.log("This should happen before the other console logs");

It is showing undefined because at that point in the code, it IS undefined. AJAX is Asynchronous, meaning it doesn't complete before code after it get's ran. The responseText will be undefined until the ajax call is complete. Here's an example:

var xhr = $.get('cfc/partsDefinition.cfc',{
                method: 'checkValid',
                search: 'some search string'
            }
        ).done(function(data){
            console.log("xhr",xhr);
            console.log("xhr.responseText",xhr.responseText);
            console.log("data",data);
        });
console.log("This should happen before the other console logs");
云仙小弟 2025-01-04 02:22:42

同意 - 当它不存在时你可能会抛弃它;尝试添加一些错误处理:

        $.get('cfc/partsDefinition.cfc',{
                method: 'checkValid',
                search: 'some search string'
            },
            function(data, textStatus, jqXHR){ // Success Case
                console.log(data);
                console.log(jqXHR);
            }
        );

        $('.errorContainer').ajaxError(function(event, jqXHR, ajaxSettings, thrownError){
                    $(this).append('ohhhhz noooz - ajax error');
            console.log(event);
            console.log(jqXHR);
            console.log(ajaxSettings);
            console.log(thrownError);
        });

在 jQuery 中,get() 简写没有任何内置的错误处理 - 它只是抑制它。因此,如果您想捕获某些内容,则需要创建自己的错误处理程序。

http://api.jquery.com/ajaxError/

Agreed - you're probably dumping it when it's not there; try adding some error handling:

        $.get('cfc/partsDefinition.cfc',{
                method: 'checkValid',
                search: 'some search string'
            },
            function(data, textStatus, jqXHR){ // Success Case
                console.log(data);
                console.log(jqXHR);
            }
        );

        $('.errorContainer').ajaxError(function(event, jqXHR, ajaxSettings, thrownError){
                    $(this).append('ohhhhz noooz - ajax error');
            console.log(event);
            console.log(jqXHR);
            console.log(ajaxSettings);
            console.log(thrownError);
        });

In jQuery the get() shorthand doesn't have any built in error handling - it just supresses it. So if you're trying to catch something you need to make your own error handlers.

http://api.jquery.com/ajaxError/

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