function tinyurl() {
jQuery.ajax({
url: 'http://tinyurl.com/api-create.php?url=http://xxxxxxxxxxx/',
dataType: 'script',
success: function(response) {
welcomemsg(response.id);
}
});
}
function namie() {
jQuery.ajax({
url: 'https://xxxxxx.com/names?user=random,
dataType: 'jsonp',
success: function(data) {
welcomemsg(data);
}
});
}
function welcomemsg() {
jQuery.ajax({
url: 'https://xxxxx.com/posts?walmessage=Hello ' + data.name + ' your post has been posted '+ response.id +',
dataType: 'script',
success: function() {
}
});
}
我希望获得tinyurl
和namie
函数的响应,然后在函数welcomemsg
中使用它们。
我尝试使用参数,但没有成功。
答案 0 :(得分:2)
使用$.ajax
返回的承诺。
var p1 = jQuery.ajax({url:'http://tinyurl.com/api-create.php?url=http://xxxxxxxxxxx/', dataType:'script'})
var p2 = jQuery.ajax({url:'https://xxxxxx.com/names?user=random', dataType:'jsonp'})
$.when(p1, p2).then(function (response, data) {
welcomemsg(response[0], data[0]);
});
function welcomemsg(response, data){
jQuery.ajax({
url:'https://xxxxx.com/posts?walmessage=Hello ' + data.name + ' your post has been posted '+ response.id ,
dataType:'script',
success:function(){
}
});
}