简体   繁体   English

如何使用return函数之外的Ajax POST调用中返回的数据?

[英]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.

相关问题 在我的情况下,如何在 AJAX 回调 function 之外访问从 ajax 调用返回的数据? - How can I access data returned from ajax call outside AJAX callback function in my case? 如何从PHP提取返回数据以提供给我的AJAX POST调用函数? - How do I pull in the return data from PHP to give to my AJAX POST call function? 使用从 API 调用返回的数据 Promise.then() function - Use data returned from API call outside of Promise.then() function 如何使用从jQuery的POST或GET方法返回的函数之外的数据? - How to use data outside of function returned from POST or GET method of jquery? 我如何在Ajax发布呼叫中发送不同类型的数据 - How can I send different types of data in ajax post call 稍后我如何在另一个函数中使用 promise 返回的数据 - How can i use promise returned data in another function later on 我如何在.ajax()之外使用ajaxSuccess() - How can I use ajaxSuccess() outside of .ajax() 我可以在ajax调用返回的PartialView上使用ValidationSummary吗? - Can I use ValidationSummary on a PartialView returned by ajax call? 我有连续的ajax电话,我怎么能用返回的数据构造一个表 - i have continuous ajax call how can i construct a table with the returned data 在HighCharts中,我无法显示从ajax调用返回的数据 - In HighCharts, I Can't display data returned from ajax call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM