ajax get response error message
I'm using ajax inisde my jsp page. after getting to the servlet, I want to send back a respone with a message. at first I used "response.sendError(500 , "this is 开发者_运维百科a test msg"); inside my servlet but when I tried getting the error message inside the ajax error function , I used xHR.responeText and got an html string that did contain the message , but it is not good for me , I want to get only the error message. I also tried setStatus(500) , and contenttype - xml , but I still couldn't get the error message.
How can this be done? I want to send error code number 500 , but each time I want to send it with diffrent message , and then read it inisde the ajax callback function.
Thank's In Advance.
Assuming I understand your question right You want to recieve the error message as a plain string without html tags?
If this is the case you should write the text to the response and set the mime type (contentType to "text/plain).
So something like.
PrintWriter writer = response.getOut();
writer.print(errorMessage);
response.setStatus(500);
return
Make sure you haven't aready written to the response when you do this.
error: function(error) {
targetNode.innerHTML = " text to display" + error;
}
with jQuery
$.ajax({
type: 'POST',
url: url_,
data: urlParams_,
dataType: 'text',
async: false,
success: function(result_) {
if (result_.length > 0 ){
try{
result_ = $.parseJSON(result_);
result = result_;
}catch (e){
alert(e);
}
}
}
});
In your JS you need the read the xHR.status. Not the xHR.responeText. xHR.status should contain 500.
精彩评论