我在URL中使用不同的路径和查询对相同的API进行了不同的Ajax调用,但是与相同的jsonpCallback我无法更改
当我拨打一个Ajax网址时 - 一切都很好!
但是当我需要从多个获得响应时,我得到错误,导致他们立即执行。我尝试了回调,我尝试了“async:false”,我尝试“在何时返回ajax,然后完成等等 - 没有像我预期的那样工作:(
function timenow() {
var a = new Date,
b = a.getMinutes();
b < 10 && (b = "0" + b);
var c = a.getHours();
c > 12 && (c -= 12);
var d = c + ":" + b;
return d
}
function ajaxCall(p, callback) {
if (p == 1) {
url = 'https://api.vk.com/method/wall.get?owner_id=-93100300&offset=0&count=1&filter=owner&callback=logResults'
} else {
url = "https://api.github.com/users/jeresig?callback=logResults"
}
return $.ajax({
url: url,
dataType: "jsonp",
jsonp: false,
jsonpCallback: "logResults",
cache: true,
}).done(function(r) {
callback(r);
});
}
$.when(
ajaxCall(0, function(r) {
console.log(r);
console.log(r.data.avatar_url + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r);
console.log(r.response[1].id + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.bio + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].id + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.blog + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].from_id + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.company + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].to_id + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.created_at + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].date + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.email + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].post_type + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.events_url + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].text + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.followers + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].is_pinned + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.followers_url + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].reposts.count + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.following + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].likes.count + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.following_url + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].comments.count + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.gists_url + ' ' + timenow());
})
).done(console.log('Ajaxing done ' + timenow()));
我认为问题在同一个jsonpCallback中,所以当我在ajax中使用回调时,它总是用新函数重写它,所以我收到错误。
P.S。仍然无法将异步设置为false,工作样本https://jsfiddle.net/ypmkn5k0/
<div id="log"></div>
<script>
$.ajaxSetup({ async: false});
$.ajax({
url: 'https://api.github.com/users/jeresig?callback=logResults',
dataType: "jsonp",
jsonp: false,
jsonpCallback: "logResults",
cache: true,
async: false,
}).done(function(r) {
$('#log').append(r.data.avatar_url+'</br>');
});
$('#log').append('Ajaxing done </br>');
</script>
P.S.S哦,我再次检查了文档,现在我明白了,非常伤心((
跨域请求和dataType:“jsonp”请求不支持 同步操作。
P.S.S.S我最后一次尝试回调,它的工作,FUK-FUK-FUK,不知道为什么它不是真正的项目,但我会尝试去实现它:
function timenow(){var a=new Date,b=a.getMinutes();b<10&&(b="0"+b);var c=a.getHours();c>12&&(c-=12);var d=c+":"+b;return d}
function ajaxCall(p,callback) {
if(p==1){
url='https://api.vk.com/method/wall.get?owner_id=-93100300&offset=0&count=1&filter=owner&callback=logResults'
} else {
url="https://api.github.com/users/jeresig?callback=logResults"
}
$.ajax({
url: url,
dataType: "jsonp",
jsonp: false,
jsonpCallback: "logResults",
cache: true,
success: function (r) {
callback(r);
},
});
}
ajaxCall(0,function(r){console.log(r); console.log(r.data.avatar_url+' '+timenow());
ajaxCall(1,function(r){console.log(r); console.log(r.response[1].id+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.bio+' '+timenow());
ajaxCall(1,function(r){console.log(r.response[1].id+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.blog+' '+timenow());
ajaxCall(1,function(r){console.log(r.response[1].from_id+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.company+' '+timenow());
ajaxCall(1,function(r){console.log(r.response[1].to_id+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.created_at+' '+timenow());
ajaxCall(1,function(r){console.log(r.response[1].date+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.email+' '+timenow());
ajaxCall(1,function(r){console.log(r.response[1].post_type+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.events_url+' '+timenow());
ajaxCall(1,function(r){console.log(r.response[1].text+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.followers+' '+timenow());
ajaxCall(1,function(r){console.log(r.response[1].is_pinned+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.followers_url+' '+timenow());
ajaxCall(1,function(r){console.log(r.response[1].reposts.count+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.following+' '+timenow());
ajaxCall(1,function(r){console.log(r.response[1].likes.count+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.following_url+' '+timenow());
ajaxCall(1,function(r){console.log(r.response[1].comments.count+' '+timenow());
ajaxCall(0,function(r){console.log(r.data.gists_url+' '+timenow());
console.log('Ajaxing done '+timenow());
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
答案 0 :(得分:1)
我看到的主要问题是在最后的.done()中。这应该包含在一个函数中。那,我认为如果你宣布你的'url'变量会更好,这样就不会混淆它的本地化。
function timenow() {
var a = new Date,
b = a.getMinutes();
b < 10 && (b = "0" + b);
var c = a.getHours();
c > 12 && (c -= 12);
var d = c + ":" + b;
return d
}
function ajaxCall(p, callback) {
var url = 'https://api.github.com/users/jeresig?callback=logResults';
if (p == 1) {
url = 'https://api.vk.com/method/wall.get?owner_id=-93100300&offset=0&count=1&filter=owner&callback=logResults'
}
return $.ajax({
url: url,
dataType: "jsonp",
jsonp: false,
jsonpCallback: "logResults",
cache: true,
}).done(function(r) {
callback(r);
});
}
$.when(
ajaxCall(0, function(r) {
console.log(r);
console.log(r.data.avatar_url + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r);
console.log(r.response[1].id + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.bio + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].id + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.blog + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].from_id + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.company + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].to_id + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.created_at + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].date + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.email + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].post_type + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.events_url + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].text + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.followers + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].is_pinned + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.followers_url + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].reposts.count + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.following + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].likes.count + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.following_url + ' ' + timenow());
}),
ajaxCall(1, function(r) {
console.log(r.response[1].comments.count + ' ' + timenow());
}),
ajaxCall(0, function(r) {
console.log(r.data.gists_url + ' ' + timenow());
})
).done(function() { console.log('Ajaxing done ' + timenow()); });
如果你打算使用async:false参数,那么在.when / .done中包含所有这些参数是没有意义的,如果你知道你不能异步调用它们,那么我会建议你删除.when逻辑,然后使用async:false属性顺序调用该函数。
看到你的jsonpCallback也会有所帮助。如果您有任何可被视为共享的变量或资源,那么这也可能是一个问题。