2016-02-12 59 views
0

Im做與jQuery後到一個Web服務,我也得到一個SOAP響應,這樣打印SOAP響應使用jQuery

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
<s:Body> 
    <spGetUserResponse> 
     <spGetUserResult xmlns:a="http://schemas.datacontract.org/2004/07/Mobilu.SharePoint.Services.MessageContract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
      <a:ReturnCode i:nil="true"/> 
      <a:ReturnMessage>OK</a:ReturnMessage> 
      <a:User xmlns:b="http://schemas.datacontract.org/2004/07/Mobilu.SharePoint2013Gapp.DataContract"> 
       <b:Email/> 
       <b:Id>3</b:Id> 
       <b:LoginName>WIN-RICM5VNKO1F\Administrator</b:LoginName> 
       <b:Name>WIN-RICM5VNKO1F\Administrator</b:Name> 
      </a:User> 
     </spGetUserResult> 
    </spGetUserResponse> 
</s:Body> 

,我的帖子是這樣的

$.ajax({ 
      type: "POST", 
      headers: { 

      "SOAPAction":"urn:ISharePointGappService/spGetUser" 

      }, 

      contentType: "text/xml; charset=utf-8", 

      url: "http://localhost/WcfServiceLibrary1/Service1/", 
      data: x, 
      dataType: "xml", 
      //If the call succeeds  
      success: 
         function (response) { 

         var xmlResponse =response.documentElement; 

         var users = $(xmlResponse).find('User').text(); 
         console.log(users); 
         console.log(response); 
         obj.val(users); 


         }, 
      //If the call fails  
      error: 
         function (XMLHttpRequest, textStatus, errorThrown) { 
          obj.val(JSON.stringify(errorThrown)); 
          console.error(errorThrown); 
         } 
     }); 
    } 

和現在的代碼我現在即時印刷像這樣的東西

3WIN-RICM5VNKO1F \ AdministratorWIN-RICM5VNKO1F \管理員

我想是打印類似:

Email: 
ID: 3 
LoginName: WIN-RICM5VNKO1F\Administrator 
Name: WIN-RICM5VNKO1F\Administrator 

感謝您的幫助

回答

0

一個成功的功能,如以下應該工作:

function (response) { 
    var xmlResponse =response.documentElement; 

    var user = $(xmlResponse).find('User'); 

    var email = user.find('Email').text(); 
    var id = user.find('Id').text(); 
    var loginName = user.find('LoginName').text(); 
    var name = user.find('Name').text(); 

    console.log('Email:' + email); 
    console.log('ID:' + id); 
    console.log('LoginName:' + loginName); 
    console.log('Name:' + name); 
} 
0

你可以用這種方式:

txt="<s:Body>" + 
 
     "<spGetUserResponse>" + 
 
     "<b:Email/>" + 
 
     "<b:Id>3</b:Id>" + 
 
     "<b:LoginName>WIN-RICM5VNKO1F\Administrator</b:LoginName>" + 
 
     "<b:Name>WIN-RICM5VNKO1F\Administrator</b:Name>" + 
 
     "</spGetUserResponse>" + 
 
    "</s:Body>"; 
 

 
if (window.DOMParser) 
 
    { 
 
    parser=new DOMParser(); 
 
    xmlDoc=parser.parseFromString(txt,"text/xml"); 
 
    } 
 
else // Internet Explorer 
 
    { 
 
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); 
 
    xmlDoc.async=false; 
 
    xmlDoc.loadXML(txt); 
 
    } 
 

 
var id = xmlDoc.getElementsByTagName("Id")[0].childNodes[0].nodeValue; 
 
var LoginName = xmlDoc.getElementsByTagName("LoginName")[0].childNodes[0].nodeValue; 
 
var Name = xmlDoc.getElementsByTagName("Name")[0].childNodes[0].nodeValue; 
 

 
console.log(id); 
 
console.log(LoginName); 
 
console.log(Name);