使用jquery打印soap响应

时间:2016-02-12 17:34:33

标签: javascript jquery xaml 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

感谢您的帮助

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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);