jquery – IE9只能正确敲击F12,才能正确调用ajax

前端之家收集整理的这篇文章主要介绍了jquery – IE9只能正确敲击F12,才能正确调用ajax前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的JSP页面中有这个jQuery代码(jQuery 1.7.2):
  1. function Header() {
  2. this.add = function ( parentDiv,leftToolbar,rightToolbar ) {
  3. hbHeader = Handlebars.compile( $( "#hb-header" ).html() );
  4.  
  5. $( parentDiv ).html( hbHeader( {user:{tenantDescription:"",firstName:"",lastName:""},leftTB:null,rightTB:null } ) );
  6.  
  7. $.ajax( {
  8. url:"${pageContext.request.contextPath}/services/login/sessionUser",type:"POST",async:true,success:function ( result ) {
  9. app.user = result;
  10. var ltHtml;
  11. var rtHtml;
  12. if ( leftToolbar ) {
  13. ltHtml = new Handlebars.SafeString( leftToolbar );
  14. }
  15. if ( rightToolbar ) {
  16. rtHtml = new Handlebars.SafeString( rightToolbar );
  17. }
  18. $( parentDiv ).html( hbHeader( {user:app.user,leftTB:{
  19. html:ltHtml,hidden:leftToolbar == null
  20. },rightTB:{
  21. html:rtHtml,hidden:rightToolbar == null
  22. }
  23. } ) )
  24. },error:function( jqXHR,textStatus,errorThrown ) {
  25. alert("error in ajax");
  26. }
  27. } );
  28. }
  29. }

在执行此代码之前,将注册ajaxSend和ajaxError侦听器。像这样:

  1. $( "#log-window" ).ajaxSend( function ( event,jqXhr,ajaxOptions ) {
  2. console.log( "handling an ajax request adding username and password to the header" );
  3. jqXhr.setRequestHeader( 'username',$.cookie( 'username' ) );
  4. jqXhr.setRequestHeader( 'password',$.cookie( 'password' ) );
  5. } );
  6. $( "#log-window" ).ajaxError( function ( errorThrown,xhr,FailedReq,textStatus ) {
  7. alert("error in "+FailedReq.url);
  8. if ( textStatus == 'timeout' ) {
  9. if ( !FailedReq.tryCount ) {
  10. FailedReq.tryCount = 1;
  11. FailedReq.retryLimit = 3;
  12. }
  13. if ( FailedReq.tryCount++ <= FailedReq.retryLimit ) {
  14. //try again
  15. $.ajax( FailedReq );
  16. return;
  17. }
  18. throw 'Es wurde ' + FailedReq.retryLimit + ' Mal versucht. Die Verbindung scheint nicht zu funktionieren.';
  19. return;
  20. }
  21. if ( xhr.status == 500 ) {
  22. if ( $.cookie( 'pageRefreshed' ) == null ) {
  23. $.cookie( 'pageRefreshed','true',{ expires:10000 } );
  24. location.reload();
  25. }
  26. throw 'Der Server hatte ein Problem. Bitte melden Sie den Fehler and den Systemadministrator';
  27. } else if ( xhr.status == 401 ) {
  28. if ( FailedReq.url != "${pageContext.request.contextPath}/services/login" ) {
  29. var loginData = {
  30. username:$.cookie( 'username' ),password:$.cookie( 'password' )
  31. };
  32. loginAttempt( FailedReq,loginData );
  33. }
  34. } else {
  35. throw 'Oops! There was a problem,sorry.';
  36. }
  37. } );

整个页面可以在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中使用的简单解决方案来更新此答案:

  1. // Avoid `console` errors in browsers that lack a console.
  2. (function() {
  3. var method;
  4. var noop = function () {};
  5. var methods = [
  6. 'assert','clear','count','debug','dir','dirxml','error','exception','group','groupCollapsed','groupEnd','info','log','markTimeline','profile','profileEnd','table','time','timeEnd','timeStamp','trace','warn'
  7. ];
  8. var length = methods.length;
  9. var console = (window.console = window.console || {});
  10.  
  11. while (length--) {
  12. method = methods[length];
  13.  
  14. // Only stub undefined methods.
  15. if (!console[method]) {
  16. console[method] = noop;
  17. }
  18. }
  19. }());

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

猜你在找的jQuery相关文章