[英]JQuery can't catch exception in the Restful web serverice.
我正在使用JQuery获取ajax调用,它将不会在回调函数中捕获该异常,在java静态代码中,我强制它抛出WebApplicationException异常,但Ajax调用无法捕获它,但是它将显示在Chrome中安慰。
如何在ajax回调方法中捕获它? 谢谢。
@GET
@Path("getForm")
@Produces(MediaType.APPLICATION_JSON)
public String getForm() throws Exception {
.....
if (1==1) {
System.out.println("---exeption ---");
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
....
}
阿贾克斯电话
$.get('myurl', function(data, status) {
alert(status); // not executed at all
if (status === 'success') {
alert("success");
}
else {
alert("load failed");
}
});
在Chrome控制台中,我可以看到错误消息:401(未经授权)。
$.get
的回调函数是成功的,您必须使用.fail
来处理失败的请求。
$.get('myurl', function(data) {
// when success
}).fail(function(jqXHR, textStatus, errorThrown) {
// when fail
});
$.get('myurl', function(data) {
alert(data);
}).fail(function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.status);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.