Jquery -将Ajax jSON响应存储为变量

时间:2022-12-08 11:10:26

I'm trying to get the result of an ajax request to set in a variable which I can access outside that request. I've tried this JQuery - Storing ajax response into global variable but my variable beer remains undefined outside the $.getJSON and $.ajax functions (I tried both).

我正在尝试将ajax请求的结果设置为一个变量,我可以在该请求之外访问该变量。我尝试过这种JQuery——将ajax响应存储到全局变量中,但是我的变量beer在$之外仍然没有定义。getJSON和美元。ajax函数(我都试过了)。

Here is my code and where I am able to see the results from the console.log(beer).

这是我的代码,我可以在这里看到来自console.log(啤酒)的结果。

var beer;
$.getJSON(jsonUrl, function (json) {
    beer = json;
    console.log(beer); // returns beer
});
console.log(beer); // returns undefined

var beer = (function () {
    var result;

    $.ajax({
        url: jsonUrl,
        success: function (data) {
            result = data;
            console.log(beer); // returns beer

        }
    });
    console.log(result); // returns undefined
    if (result) return result;
})();
console.log(beer); // returns undefined

3 个解决方案

#1


17  

That's an asynchronous request, so it gets fired off, but your script doesn't wait around for a response before it moves on. If you need to wait on a ajax request to finish, try something like this:

这是一个异步请求,因此它会被触发,但是您的脚本不会在响应继续之前等待它。如果需要等待ajax请求完成,请尝试以下操作:

var beer;
$.getJSON(jsonUrl,function(json){
    beer = json;   
    checkDrink();                
});         

function checkDrink() {
    console.log(beer);
}   

#2


1  

The problem is that you're trying to access the data before it actually comes back from the server, the 'success' function is actually a callback that gets called when the ajax call finishes successfully. The $.ajax (or $.get) functions return inmediatly...

问题是您试图在数据从服务器返回之前访问它,“success”函数实际上是一个回调函数,在ajax调用成功完成时调用它。美元的。ajax(或$.get)函数返回到mediatly…

You would need to somehow signal to the interested functions that you got the data into the 'beer' var inside your success callback

您需要以某种方式向感兴趣的函数发出信号,表明您将数据放入成功回调中的“beer”var中

#3


1  

Suggest the code below:

显示下面的代码:

var beer = $.ajax({
    url: jsonUrl,
    async: false,
    dataType: 'json'
}).responseJSON;

The key moments are:

关键时刻是:

  1. set async to false, to return result as variable, not call success callback asynchronious
  2. 将异步设置为false,以返回结果作为变量,而不是调用成功回调异步。
  3. set dataType to json to parse server response string as json
  4. 将数据类型设置为json,以解析服务器响应字符串为json

#1


17  

That's an asynchronous request, so it gets fired off, but your script doesn't wait around for a response before it moves on. If you need to wait on a ajax request to finish, try something like this:

这是一个异步请求,因此它会被触发,但是您的脚本不会在响应继续之前等待它。如果需要等待ajax请求完成,请尝试以下操作:

var beer;
$.getJSON(jsonUrl,function(json){
    beer = json;   
    checkDrink();                
});         

function checkDrink() {
    console.log(beer);
}   

#2


1  

The problem is that you're trying to access the data before it actually comes back from the server, the 'success' function is actually a callback that gets called when the ajax call finishes successfully. The $.ajax (or $.get) functions return inmediatly...

问题是您试图在数据从服务器返回之前访问它,“success”函数实际上是一个回调函数,在ajax调用成功完成时调用它。美元的。ajax(或$.get)函数返回到mediatly…

You would need to somehow signal to the interested functions that you got the data into the 'beer' var inside your success callback

您需要以某种方式向感兴趣的函数发出信号,表明您将数据放入成功回调中的“beer”var中

#3


1  

Suggest the code below:

显示下面的代码:

var beer = $.ajax({
    url: jsonUrl,
    async: false,
    dataType: 'json'
}).responseJSON;

The key moments are:

关键时刻是:

  1. set async to false, to return result as variable, not call success callback asynchronious
  2. 将异步设置为false,以返回结果作为变量,而不是调用成功回调异步。
  3. set dataType to json to parse server response string as json
  4. 将数据类型设置为json,以解析服务器响应字符串为json