gpt4 book ai didi

javascript - 如何正确检测 iframe 加载响应 204 No Content?

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:42 24 4
gpt4 key购买 nike

为了更清楚,我让它变得简单(所以我们假设 iframe.attr() 将在之前的加载完全完成后被调用):

var iframe = $("iframe");
var counter = 0;
var trackLoads = function(){
console.log("I was loaded:" + counter);
counter += 1;
};

iframe.load(trackLoads);

iframe.attr("src","http://stackoverflow.com/");
iframe.attr("src","http://localhost:9081/mobile/api/content/badPath"); //returns 204 No Content
iframe.attr("src","http://stackoverflow.com/");

控制台日志如下所示:

I was loaded:1
I was loaded:2

当 iframe 从 stackoverflow.com 加载新内容时,它会触发回调“trackLoads”,但 iframe 由于某些原因永远不会触发 204 No Content 的回调。

如何在 iframe 更改“src”属性后检测“204 No Content”?

最佳答案

将您之前的代码更改为:

var trackLoads = function(response, status, xhr){
console.log("I was loaded:" + counter);
counter += 1;
if ( status == "error" && xhr.status == 204) {
console.log( "Sorry but there was an error: " + xhr.status + " " + xhr.statusText );
}
};

发件人:http://api.jquery.com/load/

关于javascript - 如何正确检测 iframe 加载响应 204 No Content?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25869737/

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