我有两个需要加在一起的单独的Ajax Post请求,它们都链接到一个货币交换提要,基本上它的两个值是从不同的货币转换而来,然后在都转换成一种货币后需要加在一起,即使它们都使用相同的货币,不,我只需要将两个结果相加即可得出最终总数。
这是两个请求
var dataString = "amount=" + entireTotal + "&from=" + from + "&to=" + to;
//Lets Get the exchange rates from our total
$.ajax({
type: "POST",
url: "http://fileserver/website/modules/mod_calculation/js/currency.php",
data: dataString,
success: function(data){
$('#inputresult').show();
//Put received response into result div
$('#inputresult').html(data);
}
});
var dataString = "amount=" + amountGel + "&from=" + fromGel + "&to=" + toGel;
$.ajax({
type: "POST",
url: "http://fileserver/website/modules/mod_calculation/js/currencygel.php",
data: dataString,
success: function(data){
$('#resultsgel').html(data);
//Show results div
$('#resultsgel').show();
//Put received response into result div
}
});
任何帮助都将不胜感激..谢谢:)
最佳答案
var request1 = $.ajax({ ... }),
request2 = $.ajax({ ... });
$.when(request1, request2).then(function(response1, response2) { ... })
进一步阅读:
关于如何在jQuery中使用Promise的好文章:http://joseoncode.com/2011/09/26/a-walkthrough-jquery-deferred-and-promise/
还有official documentation pages。
类似于您所需的小型抽象示例(抱歉,没有足够的时间在那里创建ajax请求,但它们的行为完全相同):http://jsfiddle.net/Y26zd/