IE9仅在击中F12时正确调用ajax

我的JSP页面(jQuery 1.7.2)中有这个jQuery代码:

function Header() { this.add = function ( parentDiv, leftToolbar, rightToolbar ) { hbHeader = Handlebars.compile( $( "#hb-header" ).html() ); $( parentDiv ).html( hbHeader( {user:{tenantDescription:"", firstName:"", lastName:""}, leftTB:null, rightTB:null } ) ); $.ajax( { url:"${pageContext.request.contextPath}/services/login/sessionUser", type:"POST", async:true, success:function ( result ) { app.user = result; var ltHtml; var rtHtml; if ( leftToolbar ) { ltHtml = new Handlebars.SafeString( leftToolbar ); } if ( rightToolbar ) { rtHtml = new Handlebars.SafeString( rightToolbar ); } $( parentDiv ).html( hbHeader( {user:app.user, leftTB:{ html:ltHtml, hidden:leftToolbar == null }, rightTB:{ html:rtHtml, hidden:rightToolbar == null } } ) ) }, error:function( jqXHR, textStatus, errorThrown ) { alert("error in ajax"); } } ); } } 

在执行此代码之前,会注册ajaxSendajaxError侦听器。 像这样:

  $( "#log-window" ).ajaxSend( function ( event, jqXhr, ajaxOptions ) { console.log( "handling an ajax request adding username and password to the header" ); jqXhr.setRequestHeader( 'username', $.cookie( 'username' ) ); jqXhr.setRequestHeader( 'password', $.cookie( 'password' ) ); } ); $( "#log-window" ).ajaxError( function ( errorThrown, xhr, failedReq, textStatus ) { alert("error in "+failedReq.url); if ( textStatus == 'timeout' ) { if ( !failedReq.tryCount ) { failedReq.tryCount = 1; failedReq.retryLimit = 3; } if ( failedReq.tryCount++ <= failedReq.retryLimit ) { //try again $.ajax( failedReq ); return; } throw 'Es wurde ' + failedReq.retryLimit + ' Mal versucht. Die Verbindung scheint nicht zu funktionieren.'; return; } if ( xhr.status == 500 ) { if ( $.cookie( 'pageRefreshed' ) == null ) { $.cookie( 'pageRefreshed', 'true', { expires:10000 } ); location.reload(); } throw 'Der Server hatte ein Problem. Bitte melden Sie den Fehler and den Systemadministrator'; } else if ( xhr.status == 401 ) { if ( failedReq.url != "${pageContext.request.contextPath}/services/login" ) { var loginData = { username:$.cookie( 'username' ), password:$.cookie( 'password' ) }; loginAttempt( failedReq, loginData ); } } else { throw 'Oops! There was a problem, sorry.'; } } ); 

整个页面可以在http://alpha.sertal.ch:8181/VisionWeb/data-details/#data:12300923下访问

如果你愿意,你甚至可以登录。 用户:alsoft03密码:密码

当第一次进行ajax调用时应该发生什么,是401错误,因为用户没有进入。这就是IE失败的地方。 调用ajaxSend侦听器但不调用ajaxError 。 如果我在$ .ajax本身上设置了一个错误回调,那么它也不会被调用。

当我用真正的浏览器(FF,Chrome,Opera)打开页面时,它工作正常并要求输入密码。 除非您按F12,否则IE不会,在这种情况下,网站也会按预期工作。

这真的很奇怪。 您打开控制台加载页面,它的工作原理。 控制台关闭后,它只显示一个空标题。 页面加载后,您可以关闭控制台,它将继续加载。

我在各种机器上测试了这个并获得了相同的结果。

它让我疯狂。 我无法调试,因为如果我用F12打开调试器,它工作正常。

我知道ajax回调不是因为我把alert("I got so far")线放在不同的位置。

如果有人有线索,非常欢迎。

需要从脚本中删除console.log()所有实例,以使其在IE9中工作而不处于开发人员模式。

更新我很高兴这个答案帮助了一些人。 我想我会用HTML5 Boilerplate中使用的简单解决方案来更新这个答案:

 // Avoid `console` errors in browsers that lack a console. (function() { var method; var noop = function () {}; var methods = [ 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn' ]; var length = methods.length; var console = (window.console = window.console || {}); while (length--) { method = methods[length]; // Only stub undefined methods. if (!console[method]) { console[method] = noop; } } }()); 

将它放在您的代码之前,它应该可以帮助您避免在缺少控制台的浏览器中出现任何控制台错误。

在IE中,在开发者模式之外未定义console.log。 我喜欢做一个变量

 var console = {log: function(){}}; 

因为我没有调试。

我有这个问题,我在脚本中注释了所有的控制台引用,但它仍然在IE中失败(11)。 我发现解决方案是在我的AJAX调用中添加cache false。

 $.ajax({ **cache: false,** type : 'GET',}) 

我的所有输入都有autocomplete =“off”属性

真的它节省了我的时间,在IE 11中,我们应该把cache:false用于获取ajax来解决这个问题