2012-03-08 131 views
4

我正在使用JQuery执行Web服务调用,它是ajax函数,我无法解析返回的数据。当我提醒数据(alert($(data).find(“return”)。text())时,我看到服务器响应xml数据,如下所述,当我提醒(data)时,我得到[object XMLDocument ]。txt = $(data).find(“return”)。text()有效的给定了我的XML结构,下面是一个名字空间?我可以在萤火虫中看到完整的xml字符串。 $(数据).find( “NS1 \:回归”)。文本();适用于Chrome和Firefox,但不是Safari浏览器使用命名空间解析XML JQuery Ajax响应

index.js:

$(function() { 
$.ajax({ 
       url: url, 
       success: function (data) { 
        var ndx = 0, 
         row, 
         **txt = $(data).find("return").text(),** 
         xml = unescape(txt), 
         xmlDoc = $.parseXML(xml), 
         firstrow = $(xmlDoc).find(
           "results").children(":first"); 

        // populate the table based on the results returned by 
        // the web service 
        $("table.results thead").empty(); 
        $("table.results tbody").empty(); 
        row = $("<tr/>"); 

        row.append($("<th/>").text("#").addClass("ndx")); 
        firstrow.children().each(function() { 
         row.append($("<th/>").text(this.nodeName)); 
        }); 
        row.appendTo($("table.results thead")); 

        $(xmlDoc).find("row").each(function() { 
         row = $("<tr/>"); 
         row.append($("<td/>").text(ndx + 1).addClass("ndx")); 
         $(this).children().each(function() { 
          row.append($("<td/>").text($(this).text())); 
         }); 
         row.appendTo($("table.results tbody")); 
         ndx++; 
        }); 

        // clear the table if no results were returned 
        if (ndx == 0) { 
         // no rows returned 
         $("table.results thead").empty(); 
         $("table.results tbody").empty(); 
        } 

        statusNotice("Records Returned: " + ndx); 
       }, 
       error: function(XMLHttpRequest, textStatus, errorThrown) { 
        // display the error returned by the web service 
        var xmlDoc = $(XMLHttpRequest.responseXML); 
        statusError(xmlDoc.find("Text").text()); 
       },   
       complete: function(XMLHttpRequest, textStatus) { 
        // hide the busy dialog 
        $("#busy-dlg").dialog("close"); 
       } 
      }); 
     }); 

的index.html: 演示

<script type="text/javascript" src="js/jquery-1.6.4.min.js"></script> 
<script type="text/javascript" src="js/jquery-ui-min.js"></script> 
<script type="text/javascript" src="js/jquery.layout-latest.js"></script> 
<script type="text/javascript" src="js/index.js"></script> 
</head> 
<body> 
//table displaying results from ajax call here 
</body> 
</html> 

XML:

<ns1:executeResponse xmlns:ns1="http://sqlws.test.com"> 
<ns1:return> 
    <results> 
     <row> 
      <attribute1>value1</attribute1> 
      <attribute2>value2</attribute2> 
     </row> 
     <row> 
      <attribute1>value1</attribute1> 
      <attribute2>value2</attribute2> 
     </row> 
    </results> 
</ns1:return> 
</ns1:executeResponse> 
+0

XML结构无效,''不匹配''(在结束标记中缺少't')。 – 2012-03-08 23:16:00

+0

@RobW - 现在这是一个错字。 – c12 2012-03-08 23:22:19

回答

9

当一个元素由一个命名空间前缀,你还添加了命名空间:

  • .find('ns1:return')不起作用,因为:被jQuery用作伪选择器。
  • .find('ns1\:return')也不起作用,因为字符串中的单个反斜杠用作转义字符。 "ns1\:return"变成"ns1:return",它等于上一个。
  • .find('ns1\\:return')应该被使用。双反斜杠用于逃避冒号。

看来最后的解决方案在IE和Firefox中运行良好,但不是Opera,Chrome或Safari。为了获得最大的兼容性,请使用jQuery选择器,并且不要使用假前缀,即。 "ns1\\:return, return"而不是普通的ns1\\:return

演示:http://jsfiddle.net/5BQjv/51/

// For example, this is the result: 
var data = '<ns1:executeResponse xmlns:ns1="http://sqlws.test.com">' + 
       '<ns1:return>' + 
        '<results> <row> ... </row> </results>' + 
       '</ns1:return>' + 
      '</ns1:executeResponse>'; 

// The very first thing is to parse the string as XML. NOT later! 
var $xmlDoc = $($.parseXML(data)); 

// Then, look for the element with the namespace: 
var $txt = $xmlDoc.find('ns1\\:return, return'); 

// No need to use unescape or something, just use DOM manipulation: 
// `results` is the immediate child. Don't use .find, but .children 
var $firstrow = $txt.children("results").children(":first"); 

正如你可能已经注意到,我前面有一个美元标志一些变量。这是一个约定,在jQuery对象前加一个美元符号,以避免开发过程中/之后的混淆。

+0

辉煌。谢谢你救了我几个小时,可能没有结果。 – 2012-10-06 02:01:54

+0

这很棒,但是是假的。你的解决方案在命名空间PREFIXES上进行查询,这与名称空间不同。 'ns1'是前缀,在你的情况下,它指的是名字空间'http:// sqltest.test.com'。到目前为止,我还没有看到任何实际上正确处理名称空间的解决方案,只有解决如何处理名称空间前缀的答案。问题在于文档格式发生变化时,名称空间前缀可能会发生变化(或者完全可能是ABSENT),但从XML Infoset角度来看,文档完全相同。 – Cheeso 2013-02-22 19:07:56

+0

您的演示适用于IE10,Firefox,但不适用于Chrome。 – dizel3d 2013-08-15 06:32:34