如何从ajax.done数据()? [英] How to get data from ajax.done()?

查看:218
本文介绍了如何从ajax.done数据()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能:

$.ajax({
    url: "../../getposts.php"
}).done(function(posts) { 
    var postsjson = $.parseJSON(posts);
});

我如何使用 .done以外的变量 postsjson ()函数,或者我怎么声明它全球性的?

How do I use the variable postsjson outside the .done() function, or how do I declare it global?

我不能把它传递给另一个函数,因为我想用数组后,并没有在阿贾克斯结束。

I can't pass it to another function, because I want to use the array later on, and not when the ajax is completed.

推荐答案

如果你只是定义了外变量Ajax调用的:

If you just define the variable outside of the ajax call:

var postsjson;

$.ajax({
    url: "../../getposts.php"
}).done(function(posts) { 
    postsjson = $.parseJSON(posts);
});

然后你可以在户外使用它。同样的,如果你只是离开 VAR ,它将在全球范围宣布它,但不建议这么做。

Then you can use it outside. Likewise, if you just leave of the var, it will declare it globally, but that is not recommended.

正如刚才SLaks,您将无法立即使用您的AJAX调用获取数据,你有你等待完成函数为运行前,将被初始化为任何有用的东西。

As pointed out by SLaks, you won't be able to immediately use the data you get from the AJAX call, you have you wait for the done function to be run before it will be initialized to anything useful.

这篇关于如何从ajax.done数据()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆