2012-10-29 6 views
0

이 항목은 쉽게 찾을 수 있어야하지만이 경우 관련 문서가 제대로 작동하지 않습니다. 의 당신이 있다고 가정 해 봅시다 응답이 다음과 같은 XML과 웹 서비스 호출에서 돌아 오는 :JavaScript/Ajax에서 XMLDocument의 루트 요소 값을 얻는 방법은 무엇입니까?

<?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) 

"정의되지 않음"이 표시됩니다. 이런 젠장? 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] 
+0

'응답'을받는 방법에 대해 더 많은 코드를 보여주십시오. 'response'는'XMLDocument' 객체가 아닐 가능성이 높습니다. –

+2

'response.documentElement.textContent' 또는'response.documentElement.firstChild.nodeValue'는? 보다 포괄적 인 예가 없으면 더 나은 대답을 줄 수 없습니다. – lanzz

+0

둘 다 일했습니다, Ianzz. 감사. 답변에 댓글을 복사하면 받아 들일 것입니다. – Panzercrisis

답변

1
모두 response.documentElement.textContent

response.documentElement.firstChild.nodeValue는 당신에게 당신이 가지고있는 예를 들어, XML 문서와 텍스트 내용을 줄 것이다 여기 코드는 제공됩니다.

+0

와우, 죄송합니다. 이전에 답을 복사 할 때 보지 못했습니다. – Panzercrisis

관련 문제