在我的示例中,如何访问ajax调用之外返回的数据?

时间:2021-11-15 19:31:33

I have a function which generate ajax call to server:

我有一个对服务器生成ajax调用的函数:

function askServer(callback) = {
     $.ajax({
       // ...
       async: true, //I have to use true here
       success: function(response) {
         callback(response); //callback handle server response
       },

     });
  };

The function to handle server response:

处理服务器响应的函数:

function handleResponse(){
     var dataObject;
     askServer(function(response){

         //response is an object {car:{id:2, name:TOYOTA}}
         dataObject=response.car;
     });
    //Now, I would like to access the dataObject outside callback function
    //but if I make it like this, the dataObject value will be null because it is outside the callback

}

How can I access dataObject outside callback function as I indicated above? (the dataObject contain server response data)

如前所述,如何访问回调函数之外的dataObject ?(dataObject包含服务器响应数据)

I have to use async: true, I know I can get rid of the problem if I use async: false.

我必须使用async: true,我知道如果我使用async: false,我可以摆脱这个问题。

4 个解决方案

#1


2  

var dataObject;

function handleResponse() {
  askServer(function(response) {
    dataObject = response.car;
  });
}

#2


1  

If you "have to" do an async call then by definition you don't know how long it will be before the callback occurs. That's why the callback concept is used in that situation in the first place. All processing that needs to be done on the results of the async ajax call needs to occur in the callback (or in other functions called from the callback).

如果您“必须”执行异步调用,那么根据定义,您不知道在回调发生之前会有多长时间。这就是为什么在这种情况下首先使用回调概念。需要对异步ajax调用的结果进行的所有处理都需要在回调中(或在从回调调用的其他函数中)进行。

Other functions elsewhere on your page can test to see whether the results have come in yet - if the callback stores the result in a globally declared variable it obviously won't be usable until after the callback actually happens but in the mean time other code can test if that variable is null or undefined: if it is then you're still waiting for a response, but if there's a value you know the response has occurred.

其他功能页面上其他地方可以测试结果是否还进来——如果回调将结果存储在一个全局声明变量,它显然不会使用回调之前实际发生,但同时其他代码可以测试,如果变量为null或未定义的:如果是那么你仍在等待响应,但是如果有一个值你知道发生的反应。

#3


1  

This is a matter of assigning the values AFTER the callback returns. Any attempt at doing it before will only cause an "undefined" quirk.

这是在回调返回后分配值的问题。以前任何尝试都只会导致“未定义”的怪癖。

Solution: Define your function outside of your .ajax call, then in your .ajax success function - call the function you defined PASSING the data that is returned.

解决方案:在.ajax调用之外定义函数,然后在.ajax成功函数中调用定义的函数,传递返回的数据。

If this isn't what you were looking for, please tell us how you resolved your issue. Also, your code is bit confusing, and you have a comma in your function(response). Perhaps you can clean it up if you have the solution to your original question, so others can find this post more useful.

如果这不是你想要的,请告诉我们你是如何解决你的问题的。而且,您的代码有点混乱,并且您的函数(响应)中有一个逗号。如果你对你最初的问题有了答案,也许你可以把它清理干净,这样其他人就会发现这篇文章更有用。

BTW - async = true is default ;)

异步= true是默认;)

Cheers.

欢呼。

#4


-1  

Declare

声明

var dataObject;

outside of the function as a global level variable. Then you can access it from any other function.

在函数之外作为全局级变量。然后您可以从任何其他函数访问它。

#1


2  

var dataObject;

function handleResponse() {
  askServer(function(response) {
    dataObject = response.car;
  });
}

#2


1  

If you "have to" do an async call then by definition you don't know how long it will be before the callback occurs. That's why the callback concept is used in that situation in the first place. All processing that needs to be done on the results of the async ajax call needs to occur in the callback (or in other functions called from the callback).

如果您“必须”执行异步调用,那么根据定义,您不知道在回调发生之前会有多长时间。这就是为什么在这种情况下首先使用回调概念。需要对异步ajax调用的结果进行的所有处理都需要在回调中(或在从回调调用的其他函数中)进行。

Other functions elsewhere on your page can test to see whether the results have come in yet - if the callback stores the result in a globally declared variable it obviously won't be usable until after the callback actually happens but in the mean time other code can test if that variable is null or undefined: if it is then you're still waiting for a response, but if there's a value you know the response has occurred.

其他功能页面上其他地方可以测试结果是否还进来——如果回调将结果存储在一个全局声明变量,它显然不会使用回调之前实际发生,但同时其他代码可以测试,如果变量为null或未定义的:如果是那么你仍在等待响应,但是如果有一个值你知道发生的反应。

#3


1  

This is a matter of assigning the values AFTER the callback returns. Any attempt at doing it before will only cause an "undefined" quirk.

这是在回调返回后分配值的问题。以前任何尝试都只会导致“未定义”的怪癖。

Solution: Define your function outside of your .ajax call, then in your .ajax success function - call the function you defined PASSING the data that is returned.

解决方案:在.ajax调用之外定义函数,然后在.ajax成功函数中调用定义的函数,传递返回的数据。

If this isn't what you were looking for, please tell us how you resolved your issue. Also, your code is bit confusing, and you have a comma in your function(response). Perhaps you can clean it up if you have the solution to your original question, so others can find this post more useful.

如果这不是你想要的,请告诉我们你是如何解决你的问题的。而且,您的代码有点混乱,并且您的函数(响应)中有一个逗号。如果你对你最初的问题有了答案,也许你可以把它清理干净,这样其他人就会发现这篇文章更有用。

BTW - async = true is default ;)

异步= true是默认;)

Cheers.

欢呼。

#4


-1  

Declare

声明

var dataObject;

outside of the function as a global level variable. Then you can access it from any other function.

在函数之外作为全局级变量。然后您可以从任何其他函数访问它。