jQuery 如何在 Ajax 调用之外使用来自 ajax 成功函数的响应数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39637741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:13:33  来源:igfitidea点击:

How to use response data from ajax success function out of Ajax call

jqueryajax

提问by Lusine Martirosyan

I have question, When I make ajax call, and in success function I get json data, I can't use it out of success function

我有一个问题,当我进行 ajax 调用时,在成功函数中我得到了 json 数据,我不能在成功函数中使用它

 $.ajax({
        type: 'GET',
        url: url,
        dataType: 'json',
        success: function (response) {
            getData[name] = response;

        }
    });
 alert(getData[name]);

My question is how to work with getData out of ajax call

我的问题是如何在 ajax 调用中使用 getData

回答by Amr Magdy

The problem is that by default Ajax request is async
which means that ajax will start the request then execute: alert(getData[name]);then finish the request in the background and call success function.

问题是默认情况下 Ajax 请求是异步的
,这意味着 ajax 将启动请求然后执行:alert(getData[name]);然后在后台完成请求并调用成功函数。

so actually the alert will execute before success function. and to do what you want you have to tell ajax not to execute any thing before it done, in other ward set async: false
Second thing you have to declare the variable outside the ajax scope so you can access it outside ajax

所以实际上警报将在成功功能之前执行。并做你想做的事情,你必须告诉 ajax 在它完成之前不要执行任何事情,在其他病房设置async: false
第二件事你必须在 ajax 范围之外声明变量,以便你可以在 ajax 之外访问它

The final code will be :

最终代码将是:

var getData;
$.ajax({
        type: 'GET',
        url: url,
        dataType: 'json',
        async: false,
        success: function (response) {
            getData[name] = response;

        }
    });
 alert(getData[name]);

回答by Rock Rahul

You have to declare that variable getData[name] above the ajax call so you can value after it.

您必须在 ajax 调用上方声明该变量 getData[name] ,以便您可以在它之后进行赋值。

var getData;
$.ajax({
        type: 'GET',
        url: url,
        dataType: 'json',
        success: function (response) {
            getData[name] = response;

        }
    });

回答by Justin Farrugia

async false should be avoided. Play along the async and use events like .done to handle the response. This makes sure that the response is handled irrelevant of where the control is at the time of the callback. You don't care.

应避免异步错误。继续异步并使用 .done 之类的事件来处理响应。这确保响应的处理与回调时控件的位置无关。你不在乎。

回答by Shemil R

Use the property 'async:flase' in code and try once

在代码中使用属性 'async:flase' 并尝试一次

回答by gre_gor

AJAX stands for

AJAX 代表

asynchronousJavaScript and XML.

异步JavaScript 和 XML。

When you call alertthe AJAX call isn't finished, that's why getData[name]isn't set.

当您调用alertAJAX 时,调用未完成,这getData[name]就是未设置的原因。

You have to call alert(or do anything with it) inside the success callback.

您必须alert在成功回调中调用(或对其执行任何操作)。

$.ajax({
    type: 'GET',
    url: url,
    dataType: 'json',
    success: function (response) {
         getData[name] = response;
         alert(getData[name]);
    }
});