jQuery 将ajax响应数组存储到变量中以备后用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13011313/
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
Storing ajax response array into a variable for later usage
提问by Micky
Hi I need to store an AJAX Response into two variables x and y or into a array. My AJAX response is a array. I am able to see the data but only with and alert into che call. I need the data outside the ajax call
嗨,我需要将 AJAX 响应存储到两个变量 x 和 y 中或存储到一个数组中。我的 AJAX 响应是一个数组。我能够看到数据,但只能看到并提醒 che 呼叫。我需要ajax调用之外的数据
var x;
var y;
$.ajax({
url: 'ajaxload.php',
dataType: "json",
success: function (data) {
x = data.posX;
y = data.posX;
alert(x + " " + y); // I can se data but I need outside ajax call
}
});
回答by tftd
If I understand correctly, you want to reuse the ajax response later within your code. If that's the case, your current code wouldn't work because by default, the javascript engine doesn't wait for the response of ajax requests. In other words the code below won't work:
如果我理解正确,您希望稍后在代码中重用 ajax 响应。如果是这种情况,您当前的代码将无法工作,因为默认情况下,javascript 引擎不会等待 ajax 请求的响应。换句话说,下面的代码将不起作用:
<script type="text/javascript">
$(document).ready(function(){
var x;
var y;
$.ajax({
url: 'ajaxload.php',
dataType: "json",
success: function(data) {
x= data.posX;
y= data.posX;
alert (x+" "+y); // I can se data but I need outside ajax call
}
});
alert(x+" "+y); // You won't see anything, because this data isn't yet populated. The reason for this is, the "success" function is called when the ajax request has finished (it has received a response).
})
</script>
You need to wait for the ajax response. To do that with jQuery you need to slightly modify your code:
您需要等待 ajax 响应。要使用 jQuery 做到这一点,您需要稍微修改您的代码:
<script type="text/javascript">
$(document).ready(function(){
var data = $.parseJSON($.ajax({
url: 'ajaxload.php',
dataType: "json",
async: false
}).responseText); // This will wait until you get a response from the ajax request.
// Now you can use data.posX, data.posY later in your code and it will work.
var x = data.posX;
var y = data.posY;
alert(x+" "+y);
alert(data.posX+" "+data.posY);
});
</script>
回答by Always Sunny
you can store the ajax response in a global array for further use in other javascript function
您可以将 ajax 响应存储在全局数组中,以便在其他 javascript 函数中进一步使用
var ajaxResult=[];
$(document).ready(function(){
$.ajax({
url: 'ajaxload.php',
async:true,
dataType: "json",
success: function(data)
{
ajaxResult.push(data);
}
});
});
otherJsfunc()
{
console.log(ajaxResult);
}
回答by Nevin
if you decalre the variable you can access the value out side of ajax.
如果你 decalre 变量,你可以访问 ajax 之外的值。
eg:
例如:
<script type="text/javascript">
var x = '';
var y = '';
function sendAjax()
{
// your ajax call
x= data.posX;
y= data.posX;
}
sendAjax();
</script>
if the ajax works good you can acces the variable xand yglobaly
如果 ajax 运行良好,您可以访问变量x和yglobaly
回答by Asad Saeeduddin
If you are running this code within a document ready handler, your x and y variables are not truly global. Try window.x=''; window.y='';
如果您在文档就绪处理程序中运行此代码,则您的 x 和 y 变量并不是真正的全局变量。尝试window.x=''; window.y='';