您的位置:首页 > 其它

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

2013-04-22 17:41 405 查看
<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",

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( { "async": false } );
var result = $.getJSON('myUrl');
$.ajaxSetup( { "async": true } );
return result;
}

resourceResponse.setContentType("application/json;charset=utf-8");

resourceResponse.getWriter().print("{\"result\":\"ok\"}");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Ajax
相关文章推荐