【问题标题】:Display XML error received from server in a div在 div 中显示从服务器接收到的 XML 错误
【发布时间】:2012-12-04 13:02:43
【问题描述】:

我有一个 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 中显示相同的内容,则什么也不会发生,我希望它在那里显示。

【问题讨论】:

  • 什么是错误????“错误文本”
  • 当服务器端出现问题时,我抛出一个包含一些 XML 的自定义异常,我想在 div 中显示该 XML...例如,如果用户引入的 ISBN 包含字母,我将收到 400 Bad RequestISBN 必须只包含数字!...我确实在控制台中收到此消息

标签: jquery ajax web-services client client-server


【解决方案1】:

问题是我试图将一个字符串序列化为字符串,因为 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);
  }
});
}

【讨论】:

    【解决方案2】:

    服务器并从服务器获得响应我也得到了同样的东西。您只需要检查它的错误或响应。为此,您可以使用 javascript xquery 或获取使用。

      //suppose you get response in variable 'xml_response'.
     var res = xml_response.getElementByTagName('errorMessage');
     //if result exist then it is error other wise it is not error.
     if ( res[0] ){
      alert ( 'error occur on server side. ');
      return;
     }else{
     //show you record in div.
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      相关资源
      最近更新 更多