I have a web service from which I receive XML response. In jQuery I have the following, for getting a certain book:
function getBookByIsbn() {
if($("#getAndDeleteIsbn").val() == '')
{
alert("Please provide the ISBN");
return false;
}
$.ajax({
dataType: 'xml',
type: 'GET',
url: 'http://localhost:8080/library/books/' + $("#getAndDeleteIsbn").val(),
success: function (data) {
var string;
if (window.ActiveXObject){
string = data.xml;
}
else
{
string = (new XMLSerializer()).serializeToString(data);
}
$("#messageBox").text(string);
},
error: function (xhr, status, thrownError) {
var string;
if (window.ActiveXObject){
string = thrownError.xml;
}
else
{
string = (new XMLSerializer()).serializeToString(thrownError);
}
$("#messageBox").text(string);
}
});
}
Now, when the request is successful, the message is displayed, but when I receive an error, the content will not be displayed. What am I doing wrong?
EDIT: Someone adviced me to print all the three parameters in the console, so I’ve found out that actually the xhr parameter contains what I need. The problem now is that if I try to create an alert(xhr.responseText), the alert window contains the desired message, but if I want to display the same thing inside the div, nothing happens, and I want it to be displayed there.
The problem was that I tryed to serialize a string to string, because xhr.responseText is a string. To solve the problem, instead of xhr.responseText, it should be xhr.responseXML.
Here is the code: