如何将ajax响应保存到全局变量以供重用 [英] How do I save ajax response to global variable for reuse

查看:86
本文介绍了如何将ajax响应保存到全局变量以供重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在javascript / jquery中将ajax响应保存到全局变量中以便稍后使用。我尝试了几种方法,包括保存从调用返回的响应文本(data.responseText),但最后它仍然是空的/未定义的。



我知道保存强烈建议不要在DOM元素中进行响应。我不希望每次需要时都需要进行相同的调用。在ajax成功回拨函数之外没有简单的方法可以访问它吗?



我尝试过的方法:



How can i save ajax response to a global variable in javascript/jquery to be used later. I have tried several methods including saving the response text (data.responseText) returned from the call but at the end it is still empty/undefined.

I know saving the response in a DOM element is strongly discouraged.I don't want to make the same call every time i need i need that particular data. Is there no easy way of accessing it outside the ajax success call back function?

What I have tried:

var jsonResponse = $.ajax({
                url: 'PageMethod/GetData',
                method: 'post',
                dataType: 'json',
                data: JSON.stringify({ dataId: "xxx" }),
                contentType: 'application/json',
                success: function (data) {
                    
                    // not sure if i should return the data here on at end of the ajax call definition
                    return data.d.responseText;
                },
                error: function (ex) {
                    alert(ex.responseText);
                }
            }).responseText;

            alert(jsonResponse);

推荐答案

.ajax({
url:'PageMethod / GetData',
方法:'post',
dataType:'json',
data:JSON.stringify({dataId:xxx}),
contentType:'application / json',
成功:函数(数据){

//不确定我是否应该在ajax调用定义结束时返回数据
返回data.d .responseText;
},
错误:function(ex){
alert(ex.responseText);
}
})。responseText;

alert(jsonResponse);
.ajax({ url: 'PageMethod/GetData', method: 'post', dataType: 'json', data: JSON.stringify({ dataId: "xxx" }), contentType: 'application/json', success: function (data) { // not sure if i should return the data here on at end of the ajax call definition return data.d.responseText; }, error: function (ex) { alert(ex.responseText); } }).responseText; alert(jsonResponse);


代码不起作用的主要原因(但不仅仅是因为)ajax调用是异步的,这实际上意味着它发生在后台,或与其余代码并行。所以代码执行不会停止在ajax调用并等待响应,所以你调用ajax然后执行它的事情,然后你的代码前进到警报但是ajax调用还没有完成。



您可以让ajax等待响应,然后再继续添加async:false作为呼叫选项,但通常不需要。要存储结果,您需要执行以下操作:



The main (but not only) reason your code doesn't work is because the ajax call is asynchronous, that effectively means it happens in the background, or in parallel with the rest of the code. So code execution doesn't stop at the ajax call and wait for a response, so you call ajax which goes off and does its thing, your code then advances to the alert but the ajax call hasn't finished yet.

You can make ajax wait for a response before continuing by adding "async:false" as an option to the call, but that's generally not needed. To store the result you'd do something like

var myResponse;


.ajax({
url:'PageMethod / GetData',
方法:'post',
dataType:'json',
data:JSON.stringify({dataId:xxx}) ,
contentType:'application / json',
success:function(data){
myResponse = data.d.responseText;
},
error:function( ex){
alert(ex.responseText);
}
});
.ajax({ url: 'PageMethod/GetData', method: 'post', dataType: 'json', data: JSON.stringify({ dataId: "xxx" }), contentType: 'application/json', success: function (data) { myResponse = data.d.responseText; }, error: function (ex) { alert(ex.responseText); } });





myResponse现在是一个可以在任何地方使用的变量,但正如我所说,你必须了解异步调用的性质以及它们正在做什么,该变量只有在*成功被调用之后才可用*所以你需要牢记这一点当涉及到使用变量时。如果您有一个依赖于这些数据的进程,那么您通常会在成功事件中执行某些操作以启动该进程,因为此时变量具有数据。



myResponse is now a variable you can use anywhere, but as I said you have to understand the nature of asynchronous calls and what they're doing, that variable is only usable *after* the success has been called so you need to bear that in mind when it comes to using the variable. If you have a process that relies on this data you usually do something in the success event to kick that process off as you know at that point the variable has data.


这篇关于如何将ajax响应保存到全局变量以供重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆