jQuery AJAX。返回值未定义?

发布于 2024-12-12 04:53:14 字数 718 浏览 1 评论 0原文

我有该代码:

var s, d, p = '';

$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {
            // r contain that json data
            // {"s":"long-string","d":"string","p":"string"}
            // That served from the server with that header
            //
            // header('Cache-Control: no-cache, must-revalidate'); 
            // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            // header('Content-type: application/json');

            d = r.d;
            s = r.s;
            p = r.p;
        }
    }
);

// And here console return s as undefined
console.log(s);

知道出了什么问题吗?

I have that code:

var s, d, p = '';

$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {
            // r contain that json data
            // {"s":"long-string","d":"string","p":"string"}
            // That served from the server with that header
            //
            // header('Cache-Control: no-cache, must-revalidate'); 
            // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            // header('Content-type: application/json');

            d = r.d;
            s = r.s;
            p = r.p;
        }
    }
);

// And here console return s as undefined
console.log(s);

Any idea on what's going wrong with that ?

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

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

发布评论

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

评论(4

九厘米的零° 2024-12-19 04:53:14

$.ajax() 调用只是启动 ajax 操作。然后代码会转到您的 console.log 语句,但 ajax 调用尚未返回。因此,s 的值尚未设置。

在稍后的某个时刻,ajax 调用会返回您的结果,此时您的回调将被触发。因此,如果您想引用返回的值,您应该在回调中引用变量s

更好的方法通常是这样的:

$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {

            s = r.s;

            // Do something with s here...
        }
    }
);

如果你确实需要,你可以在回调之外引用s,但如果你这样做,你需要采取一些机制来确保< code>s 已经初始化(即您的 ajax 调用已经返回)。这引入了其他复杂性,例如同步和错误处理,并且可能使您的程序流程相当不可靠。

The $.ajax() call simply starts the ajax operation. The code then falls through to your console.log statement but the ajax call hasn't returned yet. Hence the value of s has not yet been set.

At some point later the ajax call returns your results and at that point your call back is fired. So if you want to refer to the value that is returned, you should reference the variable s inside the callback.

The better way to do this is normally like this:

$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {

            s = r.s;

            // Do something with s here...
        }
    }
);

If you really need to, you can reference s outside of the callback but if you do, you need to put some mechanism in place to make sure that s has already been initialised (i.e. that your ajax call has already returned). This introduces other complexities such as synchronisation and error handling and can make your program flow rather unreliable.

迷雾森÷林ヴ 2024-12-19 04:53:14

原因是因为当您调用 $.ajax(...); 时它会立即返回。 success 属性指定 AJAX 请求完成时调用的回调,并且仅在此处填充 s;在此之前它是未定义的。

因此,在启动 success 回调之前,基本上不要对 s 执行任何操作。

The reason is because when you call $.ajax(...); it returns immediately. The success property specifies a callback that gets called when the AJAX request finishes, and s is only populated there; it's undefined before that.

So basically don't do anything with s before the success callback is launched.

意中人 2024-12-19 04:53:14

问题是您认为代码按照您布置的方式执行。它实际上在符号上相同于:-

var s, d, p = '';
// And here console return s as undefined
console.log(s);
$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {
            // r contain that json data
            // {"s":"long-string","d":"string","p":"string"}
            // That served from the server with that header
            //
            // header('Cache-Control: no-cache, must-revalidate'); 
            // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            // header('Content-type: application/json');

            d = r.d;
            s = r.s;
            p = r.p;
        }
    }
);

你可能想要的是:-

var s, d, p = '';

$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {
            // r contain that json data
            // {"s":"long-string","d":"string","p":"string"}
            // That served from the server with that header
            //
            // header('Cache-Control: no-cache, must-revalidate'); 
            // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            // header('Content-type: application/json');

            d = r.d;
            s = r.s;
            p = r.p;
            // And here console return s as undefined
           console.log(s);
        }
    }
);

The issue is you think the code executes in the way you have it laid out. It's actually symatically identical to:-

var s, d, p = '';
// And here console return s as undefined
console.log(s);
$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {
            // r contain that json data
            // {"s":"long-string","d":"string","p":"string"}
            // That served from the server with that header
            //
            // header('Cache-Control: no-cache, must-revalidate'); 
            // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            // header('Content-type: application/json');

            d = r.d;
            s = r.s;
            p = r.p;
        }
    }
);

What you probably want is:-

var s, d, p = '';

$.ajax(
    {
        type: "POST",
        url: ajaxurl,
        data: {action: "get_info"},
        success: function(r)
        {
            // r contain that json data
            // {"s":"long-string","d":"string","p":"string"}
            // That served from the server with that header
            //
            // header('Cache-Control: no-cache, must-revalidate'); 
            // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
            // header('Content-type: application/json');

            d = r.d;
            s = r.s;
            p = r.p;
            // And here console return s as undefined
           console.log(s);
        }
    }
);
十年不长 2024-12-19 04:53:14

尝试指定数据类型喜欢
<代码>类型:“POST”,
网址:ajaxurl,
数据类型:'json',
数据:{操作:“get_info”},...

Try specifying data type Like
type: "POST",
url: ajaxurl,
dataType: 'json',
data: {action: "get_info"},
...

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