如何正确检测 iframe 已加载响应 204 No Content? [英] How to rightly detect iframe was loaded with response 204 No Content?

查看:83
本文介绍了如何正确检测 iframe 已加载响应 204 No Content?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了更清晰,我把它简化了(所以我们假设 iframe.attr() 将在上一次加载完全完成后被调用):

For more clarity I made it simple (so let's presume that iframe.attr() will be called after previous load was fully completed):

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 的回调.

When the iframe loaded new content from stackoverflow.com, It trigger the callback "trackLoads", but iframe will never trigger callback for 204 No Content from some reason.

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

How to detect "204 No Content" after iframe has changed "src" attribute?

推荐答案

把你之前的代码改成这样:

Change your previous code to this:

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/

这篇关于如何正确检测 iframe 已加载响应 204 No Content?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆