使用jQuery和ajax调用SOAP服务

时间:2014-07-17 08:05:56

标签: jquery html ajax web-services soap

我必须使用用户名,密码和我从提供商处获得的密钥来调用Web服务。

登录服务工作正常,我得到一个结果。结果附后。

在这个结果中,我必须使用一个关键来进行下一次请求。

我如何读出结果并将值存储在变量中以用于后续步骤?

非常感谢大家!!

听到代码给我结果:

$("#btnCallWebService").click(function (event) {
            var wsUrl = "http://myServerURL";

            var soapRequest =
                '<?xml version="1.0" encoding="utf-8"?> \
                <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \
                    xmlns:xsd="http://www.w3.org/2001/XMLSchema" \
                    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> \
                  <soap:Body> \
                    <WMLogin xmlns="http://tempuri.org/"> \
                      <sUserName>' + username + '</sUserName> \
                      <sPassword>' + password + '</sPassword> \
                      <sAppKey>' + appKey + '</sAppKey> \
                    </WMLogin> \
                  </soap:Body> \
                </soap:Envelope>';

            $.ajax({
                type: "POST",
                url: wsUrl,
                contentType: "text/xml",
                dataType: "xml",
                data: soapRequest,
                success: processSuccess,
                error: processError,
                contentType: "text/xml; charset=\"utf-8\"",

            });

        });


function processSuccess(data, status, req) {
        if (status == "success") {
            $("#response").text($(req.responseXML).find("WMLoginResult").text());

        }
    }


    function processError(data, status, req) {

        if (status == "error")
        alert(req.responseText + "  " + status);
    } 

我的请求后得到的回复是:

<OutputData>
   <ReturnValues>
      <Name>LoginKey</Name>
      <Value>eefe058a7da44675a25cd16bc9a83c90</Value>
      <RecordID>-1</RecordID>
   </ReturnValues>
   <ReturnValues>
      <Name>UserMenuXmlString</Name>
      <Value />
      <RecordID>-1</RecordID>
   </ReturnValues>
   <Data>
      <ID>22590</ID>
      <Username>username</Username>
      <Passwort>password</Passwort>
      <Name>Name</Name>
      <Vorname />
      <...>
      <...>
      <...>
   </Data>
   <WebServices>
      <Anwendung>Anwendung1</Anwendung>
      <ProgrammPfad>https://myServerAddress/app1</ProgrammPfad>
      <WBM_WebMethodName>WMGetAFT_Auftrag</WBM_WebMethodName>
   </WebServices>
   <WebServices>
      <Anwendung>Anwendung2</Anwendung>
      <ProgrammPfad>https://myServerAddress/app2</ProgrammPfad>
      <WBM_WebMethodName>WMGetAUA_Auftragsart</WBM_WebMethodName>
   </WebServices>
   <WebServices>
      <Anwendung>Anwendung3</Anwendung>
      <ProgrammPfad>https://myServerAddress/app3</ProgrammPfad>
      <WBM_WebMethodName>WMUpdateAFT_Auftrag</WBM_WebMethodName>
   </WebServices>
</OutputData>

我需要将值存储在标记内。 我这个例子我需要标签内的“eefe058a7da44675a25cd16bc9a83c90”

<Value>eefe058a7da44675a25cd16bc9a83c90</Value>

再次非常感谢你!

// ----------------------我的回答-------------------- &GT;

首先......谢谢所有人的答案!!!!

@tmarwen:

谢谢你!

我尝试使用你的代码并将我的函数替换为控制台日志记录命令:

function processSuccess(data, status, req) {
      if (status == "success") {
        var xmlDoc = req.responseXML // Your XML document entry
        var xmlObj = xmlDoc.documentElement;
        console.log ( 'processSuccess started' ); //executet
        try {
            console.log ( 'processSuccess --> try started' ); //executet too
          $.each(xmlObj.childNodes, function(key, val) {
              console.log ( 'processSuccess --> try --> each started' ); //executet too
            if ($(val).find("Name").text() == "LoginKey")      //I don not get true
            {
                console.log ( 'processSuccess --> try --> each if "LoginKey" found true' ); //not reached
              alert($(val).find("Value").text());

            }
            else {
                console.log ( 'Did not find "LoginKey"' ); //this will be executed
            }
          });
        }
        catch(e){
            console.log ( 'catch started' );  //not executed
        }
      }
    }

...但我没有得到所需的结果。

控制台中的日志告诉我:

[Log] processSuccess started (Login+Auftrag_stackoverflow.html, line 110)
[Log] processSuccess --> try started (Login+Auftrag_stackoverflow.html, line 112)
[Log] processSuccess --> try --> each started (Login+Auftrag_stackoverflow.html, line 114)
[Log] Did not find "LoginKey" (Login+Auftrag_stackoverflow.html, line 122)

3 个答案:

答案 0 :(得分:0)

以下是如何从XML响应中检索数据并将其存储在标记中的示例 -

http://www.think2loud.com/224-reading-xml-with-jquery/

您只需要在函数processSuccess()中进行更改。

答案 1 :(得分:0)

您可以使用以下代码检索 LoginKey 匹配元素的Value

function processSuccess(data, status, req) {
  if (status == "success") {
    var xmlDoc = req.responseXML // Your XML document entry
    var xmlObj = xmlDoc.documentElement;
    try {
      $.each(xmlObj.childNodes, function(key, val) {
        if ($(valThis).is("ReturnValues") && ($(val).find("Name").text() == "LoginKey"))
        {
          alert($(val).find("Value").text());
        }
      });
    }
    catch(e){
    }
  }
}

答案 2 :(得分:-1)

我从星期三晚上就得到了它!

今天我不在办公室。

星期一,我将与您分享解决方案。

谢谢大家。你非常帮助我得到它!

以下结果对我有用:

$.ajax({
                    type: "POST",
                    url: wsUrl,
                    contentType: "text/xml",
                    dataType: "xml",
                    data: soapRequest,
                    success: processSuccess,
                    error: processError,
                    contentType: "text/xml; charset=\"utf-8\"",
                    complete: function() {
                        $(responseData).find("ReturnValues").each(function() {
                            $("#output").append($(this).find("Value").text());
                            keyValue = $(this).find("Value").text();
                            console.log(keyValue);
                        });
                    }
                });