Javascript 如何正确地检测iframe是否加载了响应204无内容?

Javascript 如何正确地检测iframe是否加载了响应204无内容?,javascript,jquery,html,iframe,http-status-code-204,Javascript,Jquery,Html,Iframe,Http Status Code 204,为了更清楚,我将其简化(因此假设在上一次加载完全完成后将调用iframe.attr()): 控制台日志如下所示: I was loaded:1 I was loaded:2 当iframe从stackoverflow.com加载新内容时,它会触发回调“trackLoads”,但iframe不会因为某种原因触发204 No内容的回调 如何在iframe更改了“src”属性后检测“204无内容”?将以前的代码更改为: var trackLoads = function(response, stat

为了更清楚,我将其简化(因此假设在上一次加载完全完成后将调用iframe.attr()):

控制台日志如下所示:

I was loaded:1
I was loaded:2
当iframe从stackoverflow.com加载新内容时,它会触发回调“trackLoads”,但iframe不会因为某种原因触发204 No内容的回调


如何在iframe更改了“src”属性后检测“204无内容”?

将以前的代码更改为:

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

From:

您不能直接检测204,但可以使用
setTimeout
确定内容可能不会加载。我更关心的是,您的服务器返回错误的错误url代码。这与iFrame加载事件无关!加载是完全不同的。
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 );
   }
};