[英]How can I use data returned in an Ajax POST call outside the return function?
I have an Ajax POST call in jQuery that runs a PHP script and receives some data back. 我在jQuery中有一个Ajax POST调用,该调用运行PHP脚本并收到一些数据。 I want to be able to process that data outside the success function.
我希望能够在成功功能之外处理该数据。
I have instantiated a global variable components
, but this does not seem to have scope inside the success function. 我已经实例化了一个全局变量
components
,但是成功函数内部似乎没有作用域。
After reading this I tried to call a function outside the AJAX call to assign the data to a global variable components_passed
. 阅读此内容后,我尝试在AJAX调用之外调用一个函数,以将数据分配给全局变量
components_passed
。 This also does not work. 这也不起作用。
How can I get the data into a global variable? 如何将数据放入全局变量? I could just call a function to do what I want to do, but I may want to access the data again.
我可以只调用一个函数来执行我想做的事情,但是我可能想再次访问数据。
// Function to try to receive data
var components_passed;
function pass_out(data) {
components_passed = data;
}
var components = []; // create empty array for component list
if ($('#area').val) { // if area contains a value and isn't zero (i.e. an area has been selected)
$.post("/basic/get_area_components.php"
,{area: $("#area").val()}
,function(data){ components = JSON.parse(data);
console.log(components); // Show data in console - works
pass_out(data); // Call function to try and assign data to global variable
});
}
console.log(components); // Doesn't work - empty array
console.log(components_passed); // Doesn't work - empty array
your missing a little concept: AJAX means Asynchronous. 您缺少一个小概念: AJAX意味着异步。
think about what's happening in which order: 考虑一下以什么顺序发生了什么:
you declare variables, functions and so on.. 您声明变量,函数等。
then do the XHR request, 然后执行XHR请求,
then log the variable components
然后记录变量
components
then log components_passed
然后记录
components_passed
then nothing. 那什么都没有
at a certain point after, the AJAX call will return, and then, and only then, your callback is executed, your returned data saved inside components_passed
. 在之后的某个时间点,AJAX调用将返回,然后只有在执行回调之后,返回的数据才会保存在
components_passed
。
what you may want to do is wait for the call to return and then do whatever, for example logging. 您可能想要做的是等待调用返回,然后执行任何操作,例如记录日志。 But at the moment
console.log(components_passed)
is called, the data has not been loaded yet; 但是在调用
console.log(components_passed)
的那一刻,数据还没有被加载。 that's why your're printing an empty variable. 这就是为什么您要打印一个空变量的原因。
Asynchronous code can be tricky to deal with at first 起初,异步代码可能很难处理
You'll see, from the console.log statements the order in which things happen 您将从console.log语句中看到事情发生的顺序
function do_something(c) {
// here, c is components
// you can do things here with c
console.log(4);
}
if ($('#area').val) { // if area contains a value and isn't zero (i.e. an area has been selected)
console.log(1);
$.post("/basic/get_area_components.php", {area: $("#area").val()}, function(data){
console.log(3); // this line is executed once $.post has retrieved the data
var components = JSON.parse(data);
// everything you need to do with components, you must do here, or in functions called from here
do_something(components);
});
console.log(2); // as you can see, this line is executed before the callback
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.