簡體   English   中英

在JavaScript / Ajax中,如何獲取XMLDocument中根元素的值?

[英]In JavaScript/Ajax, how can you get the value of the root element in an XMLDocument?

這應該很容易查找,但是我看到的所有相關文檔在這種情況下都無法正常工作。 假設您收到來自使用以下XML的Web服務調用返回的響應:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello, World!  str=1</string>

我只想能夠提取值-世界,您好! str = 1-無需使用string.replace()或類似的東西。 如果XML存儲在一個名為response的變量中,並且是否調用了以下代碼:

alert(response.documentElement.nodeValue)

顯示的是“未定義”。 有沒有搞錯? 我只想從Web方法中獲取字符串或任何類型的普通基元(我沒有使用JSON或任何東西),但是很難找到有關如何執行此操作的文檔。 你怎么做到這一點? 謝謝!

編輯:這是代碼:

<WebMethod()> _
Public Function HelloWorld(ByVal str As Int32) As String
    Return "Hello, World!  str=" & str
End Function

<html>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script type="text/JavaScript">
    <!--
        $.ajax({
            type: "POST",
            data: "&str=1",
            dataType: "xml",
            url: "<HTTP path and filename>.asmx/HelloWorld",
            timeout: 15000,
            cache:false,
            success: function (result) {
                alert(result);
            },
            error: function() {
                alert("bad");
            }
        });
    //-->
</script>
</body>
</html>

表明:

[object XMLDocument]

response.documentElement.textContentresponse.documentElement.firstChild.nodeValue都將為您提供帶有您提供的示例XML文檔的文本內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM