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

发布于 2024-11-03 22:45:05 字数 877 浏览 4 评论 0原文

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

function askServer(callback) = {
     $.ajax({
       // ...
       async: true, //I have to use true here
       success: function(response) {
         callback(response); //callback 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

}

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

我必须使用async: true,我知道如果我使用async: false我可以解决这个问题强>。

I have a function which generate ajax call to server:

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)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绻影浮沉 2024-11-10 22:45:05
var dataObject;

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

function handleResponse() {
  askServer(function(response) {
    dataObject = response.car;
  });
}
£噩梦荏苒 2024-11-10 22:45:05

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

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

如果这不是您想要的,请告诉我们您是如何解决问题的。另外,您的代码有点混乱,并且您的函数(响应)中有一个逗号。如果您找到了原始问题的解决方案,也许您可​​以清理它,这样其他人就会发现这篇文章更有用。

顺便说一句 - async = true 是默认值;)

干杯。

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.

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 ;)

Cheers.

夏末 2024-11-10 22:45:05

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

页面上其他位置的其他函数可以测试结果是否已传入 - 如果回调将结果存储在全局声明的变量中,则显然在回调实际发生之前才可用,但同时其他代码可以测试该变量是否为空或未定义:如果是,那么您仍在等待响应,但如果有一个值,您就知道响应已经发生。

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).

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.

瀞厅☆埖开 2024-11-10 22:45:05

声明

var dataObject;

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

Declare

var dataObject;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文