<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\"}");