gpt4 book ai didi

jquery - 在 div 中显示从服务器收到的 XML 错误

转载 作者:行者123 更新时间:2023-12-01 04:17:23 26 4
gpt4 key购买 nike

我有一个 Web 服务,我可以从中接收 XML 响应。在 jQuery 中,我有以下内容,用于获取某本书:

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

现在,当请求成功时,会显示消息,但是当我收到错误时,不会显示内容。我做错了什么?

编辑:有人建议我在控制台中打印所有三个参数,所以我发现实际上 xhr 参数包含我需要的内容。 现在的问题是,如果我尝试创建一个警报(xhr.responseText),警报窗口包含所需的消息,但如果我想在 div 内显示相同的内容,则什么也不会发生,而我希望它显示在那里。

最佳答案

问题是我尝试将字符串序列化为字符串,因为 xhr.responseText 是一个字符串。为了解决这个问题,应该使用xhr.responseXML,而不是xhr.responseText。这是代码:

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 = xhr.responseXML.xml;
}
else
{
string = (new XMLSerializer()).serializeToString(xhr.responseXML);

}
$("#messageBox").text(string);
}
});
}

关于jquery - 在 div 中显示从服务器收到的 XML 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13703603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com