admin管理员组文章数量:1025251
I am using two AJAX GET requests to grab data from two different sources.
In each request I parse the response data, create a temporary array with the relevant data each item, and push this temporary array into a master array.
However, this only works if I include "async: false"
in the AJAX request, which is now deprecated.
Is there an alternative way I could write this code? (NOTE: Although I am using two AJAX requests in this code snippet, I will probably need to use a total of four in the project I am working on).
function get_results() {
$(document).ready(function() {
var master_array = [];
$.ajax({
type: "GET",
url: "http//:www.source1",
dataType: "xml",
async: false,
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
$.ajax({
type: "GET",
url: "http//:www.source2",
dataType: "xml",
async: false,
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
});
}
I am using two AJAX GET requests to grab data from two different sources.
In each request I parse the response data, create a temporary array with the relevant data each item, and push this temporary array into a master array.
However, this only works if I include "async: false"
in the AJAX request, which is now deprecated.
Is there an alternative way I could write this code? (NOTE: Although I am using two AJAX requests in this code snippet, I will probably need to use a total of four in the project I am working on).
function get_results() {
$(document).ready(function() {
var master_array = [];
$.ajax({
type: "GET",
url: "http//:www.source1.",
dataType: "xml",
async: false,
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
$.ajax({
type: "GET",
url: "http//:www.source2.",
dataType: "xml",
async: false,
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
});
}
Share
Improve this question
edited Nov 20, 2016 at 10:09
Offir
3,4914 gold badges50 silver badges80 bronze badges
asked Nov 17, 2016 at 15:00
j_devj_dev
3011 gold badge3 silver badges11 bronze badges
2
- What you do is move the second request to the end of the first request's success function. You can also use Promises. – user5734311 Commented Nov 17, 2016 at 15:03
- put your second ajax request at the end of first ajax success callback – Aman Rawat Commented Nov 17, 2016 at 15:03
1 Answer
Reset to default 4You can achieve this by using a JQuery $.promise object.
When you return $.ajax
it's actually has some built-in methods, such as
$.done() and $.when.
In your scenario you want to execute an Ajax function after the first Ajax request is done.
So we will create two variables that will represent the two Ajax requests you have in your code.
Like I mentioned earlier when you return Ajax request you actually can use it as a $.promise object and enjoy the advantages it has, such as the $.done() function.
function get_results() {
$(document).ready(function() {
var master_array = [];
var MyFirstFunction = function() {
return $.ajax({
type: "GET",
url: "http//:www.source1.",
dataType: "xml",
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
};
var MySecondFunction = function(){
return $.ajax({
type: "GET",
url: "http//:www.source2.",
dataType: "xml",
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
};
//We use it after the assignment of the variables because if would have put it before them we would got undefined, you can read more about it here:[Hoisting][4].
MyFirstFunction().done(MySecondFunction);
});
}
I am using two AJAX GET requests to grab data from two different sources.
In each request I parse the response data, create a temporary array with the relevant data each item, and push this temporary array into a master array.
However, this only works if I include "async: false"
in the AJAX request, which is now deprecated.
Is there an alternative way I could write this code? (NOTE: Although I am using two AJAX requests in this code snippet, I will probably need to use a total of four in the project I am working on).
function get_results() {
$(document).ready(function() {
var master_array = [];
$.ajax({
type: "GET",
url: "http//:www.source1",
dataType: "xml",
async: false,
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
$.ajax({
type: "GET",
url: "http//:www.source2",
dataType: "xml",
async: false,
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
});
}
I am using two AJAX GET requests to grab data from two different sources.
In each request I parse the response data, create a temporary array with the relevant data each item, and push this temporary array into a master array.
However, this only works if I include "async: false"
in the AJAX request, which is now deprecated.
Is there an alternative way I could write this code? (NOTE: Although I am using two AJAX requests in this code snippet, I will probably need to use a total of four in the project I am working on).
function get_results() {
$(document).ready(function() {
var master_array = [];
$.ajax({
type: "GET",
url: "http//:www.source1.",
dataType: "xml",
async: false,
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
$.ajax({
type: "GET",
url: "http//:www.source2.",
dataType: "xml",
async: false,
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
});
}
Share
Improve this question
edited Nov 20, 2016 at 10:09
Offir
3,4914 gold badges50 silver badges80 bronze badges
asked Nov 17, 2016 at 15:00
j_devj_dev
3011 gold badge3 silver badges11 bronze badges
2
- What you do is move the second request to the end of the first request's success function. You can also use Promises. – user5734311 Commented Nov 17, 2016 at 15:03
- put your second ajax request at the end of first ajax success callback – Aman Rawat Commented Nov 17, 2016 at 15:03
1 Answer
Reset to default 4You can achieve this by using a JQuery $.promise object.
When you return $.ajax
it's actually has some built-in methods, such as
$.done() and $.when.
In your scenario you want to execute an Ajax function after the first Ajax request is done.
So we will create two variables that will represent the two Ajax requests you have in your code.
Like I mentioned earlier when you return Ajax request you actually can use it as a $.promise object and enjoy the advantages it has, such as the $.done() function.
function get_results() {
$(document).ready(function() {
var master_array = [];
var MyFirstFunction = function() {
return $.ajax({
type: "GET",
url: "http//:www.source1.",
dataType: "xml",
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
};
var MySecondFunction = function(){
return $.ajax({
type: "GET",
url: "http//:www.source2.",
dataType: "xml",
success: function(xml) {
$(xml).find('product').each(function() {
var Average = $(this).find('Average').text();
var Price = $(this).find('Price').text();
var Name = $(this).find('Name').text();
var Url = $(this).find('Url').text();
var Image = $(this).find('Image').text();
master_array.push([Average, Price, Name, Url, Image]);
});
}
});
};
//We use it after the assignment of the variables because if would have put it before them we would got undefined, you can read more about it here:[Hoisting][4].
MyFirstFunction().done(MySecondFunction);
});
}
本文标签: javascriptAlternative to quotasync falsequot for successive AJAX callsStack Overflow
版权声明:本文标题:javascript - Alternative to "async: false" for successive AJAX calls - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745612620a2159094.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论