Mic*_*oon 18 ajax jquery javascript-events internet-explorer-9
我在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");
}
} );
}
}
Run Code Online (Sandbox Code Playgroud)
在执行此代码之前,会注册一个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.';
}
} );
Run Code Online (Sandbox Code Playgroud)
整个页面可以在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")
线放在不同的位置.
如果有人有线索,非常欢迎.
eiv*_*s88 52
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;
}
}
}());
Run Code Online (Sandbox Code Playgroud)
将它放在代码之前,它可以帮助您避免在没有控制台的浏览器中出现任何控制台错误.
归档时间: |
|
查看次数: |
7905 次 |
最近记录: |