我在我的JSP页面中有这个jQuery代码(jQuery 1.7.2):
- 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");
- }
- } );
- }
- }
在执行此代码之前,将注册ajaxSend和ajaxError侦听器。像这样:
- $( "#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以下访问
当第一次进行ajax调用时会发生什么,是401错误,因为用户没有被输入。这是IE失败的地方。 ajaxSend侦听器被调用,但是ajaxError不是。如果我在$ .ajax本身设置一个错误回调,它也不会被调用。
当我用真实的浏览器(FF,Chrome,Opera)打开页面时,它工作正常,并要求输入密码。 IE除非您按F12,否则IE不会按预期工作。
真的很奇怪您加载页面,控制台打开,它的工作原理。在控制台关闭后,它只显示一个空标题。页面加载完成后,您可以关闭控制台,并继续加载。
我在各种机器上进行了测试,得到了相同的结果。
它让我疯狂。我无法调试,因为如果我用F12打开调试器,它的工作正常。
我知道ajax回调并不是因为我把警戒(“我到目前为止”)线在各个位置。
如果有人有线索,这是非常欢迎的。
解决方法
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;
- }
- }
- }());