记录一个浪费我一天时间的ajax问题
<script type="text/javascript"> $(function() { //登录 $.ajaxSetup({ async: false }); $('#loginBtn').click(function() { login(); }); }); function login() { $.getJSON('<%=renderResponse.encodeURL(loginUrl.toString())%>',function(data){ alert(data.result); }); } </script> function login() { $.ajax({ type: "POST",url: "<%=renderResponse.encodeURL(loginUrl.toString())%>",dataType: "json",async: false,success: function(data){ alert(data.result); },error : function(XMLHttpRequest,textStatus,errorThrown){ alert("网络连接出错!"); alert(XMLHttpRequest.status); alert(XMLHttpRequest.readyState); alert(textStatus); return false; } }); } In the ajax operation just add async: false, after datatype: "json",sans-serif; line-height:18px"> and that should solve your problem. Chrome has issue handling asynchronus calls. -------------------------------------------------------------------- You can also use the following before making your call: $.ajaxSetup( { "async": false } ); I do not know the scope of the "async" property,I suspect that it is a global config. So consider whether you want to change this back to true after your synchronous call. example: 3rdPartyObject.getCustomValue = function { $.ajaxSetup( { false } ); var result = $.getJSON('myUrl'); $.ajaxSetup( { true } ); return result; }resourceResponse.setContentType("application/json;charset=utf-8"); resourceResponse.getWriter().print("{"result":"ok"}"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |