加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

记录一个浪费我一天时间的ajax问题

发布时间:2020-12-15 20:58:31 所属栏目:百科 来源:网络整理
导读: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  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"}");

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读