gpt4 book ai didi

javascript - 多个 Ajax 与完成回调具有相同的 jsonpCallback

转载 作者:行者123 更新时间:2023-11-30 00:03:47 26 4
gpt4 key购买 nike

我在 URL 中使用不同的路径和查询对相同的 API 进行了不同的 Ajax 调用,但是使用相同的 jsonpCallback 我无法更改

当我调用一个 Ajax url - 一切正常!

但是当我需要得到多个响应时,我得到了错误,导致它们立即执行。我尝试了回调,我尝试了“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()));

LOG ERROR

我认为问题在同一个 jsonpCallback 中,所以当我在 ajax 中使用回调时,它总是用新函数重写它,所以我收到错误。

附言仍然无法将异步设置为假,工作示例 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 哦,我又看了一遍文档,现在我明白了,很伤心(((

Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.

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());
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});
});

enter image description here

最佳答案

我看到的主要问题是在最后的 .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 也很有帮助。如果您有任何被认为是共享的变量或资源,那么这也可能是一个问题。

关于javascript - 多个 Ajax 与完成回调具有相同的 jsonpCallback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39301650/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com