How to rightly detect iframe was loaded with response 204 No Content?

℡╲_俬逩灬. 提交于 2020-12-10 20:44:11

问题


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/");

Console log looks like this:

I was loaded:1
I was loaded:2

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.

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


回答1:


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

From: http://api.jquery.com/load/



来源:https://stackoverflow.com/questions/25869737/how-to-rightly-detect-iframe-was-loaded-with-response-204-no-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!