我有一个jsp页面,它以json的形式发送响应。该页面是通过AJAX调用请求的。但是响应进入了Ajax错误部分而不是成功部分。
以下是我的jsp页面(getHangouts.jsp):
<%
EntityManager em = null;
try{
em = HibernateUtil.getEntityManager();
String uid1 = request.getParameter("uid1");
String uid2 = request.getParameter("uid2");
HangoutBll hb = new HangoutBll();
java.util.List<Hangout> list = hb.getAllBetweenUIds(uid1, uid2, em);
JSONArray arr = new JSONArray();
for(Hangout h: list){
JSONObject obj = new JSONObject();
obj.put("createdOn", h.getCreatedOn());
obj.put("uid", h.getUid());
obj.put("linkingUid", h.getLinkingUid());
obj.put("text", h.getText());
arr.put(obj);
}
response.setContentType("application/json");
PrintWriter outt = response.getWriter();
outt.print(arr);
}
finally{
if(em != null)
em.close();
}
%>
</body>
以下是Ajax调用:
var req_url = 'getHangouts.jsp?uid1='+uid+'&uid2='+linkingUid;
$.ajax({
type:'post',
url:req_url,
dataType: 'json',
success:function(data){
$.each(data, function (index, obj) {
var hangout = '';
if(obj.uid == uid){
hangout = '<div style="width: 100%; text-align: right;"><span style="background: #b3ecff; border-radius: 5px">'+obj.text+'</span><div>';
}
else{
hangout = '<div style="width: 100%;"><span style="background: #f2f2f2; border-radius: 5px">'+obj.text+'</span><div>';
}
$('#'+'echoText').append(hangout);
});
},
error: function(xHR) {
showMessage("<div class='alert alert-danger'><a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a> "+xHR.responseText+" </div>");
}
});
我在AJAX错误部分得到以下JSON。
[{
"uid": "Blake",
"text": "Blake: Lets have a coffee..",
"linkingUid": "Liong",
"createdOn": "2017-06-12 01:10:11.0"
}, {
"uid": "Liong",
"text": "Liong: Okay just give me 5 min",
"linkingUid": "Blake",
"createdOn": "2017-06-12 01:11:03.0"
}]
答案 0 :(得分:1)
删除getHangouts.jsp
中的所有HTML标记。返回带有响应的HTML标记可能会出现解析错误,因此调用错误块。
您还可以在控制台上检查错误:
error: function(xHR,status, error) {
console.log(error);
//console.log(xHR.responseText);
}