2017-03-17 72 views
0

我想消費sumtotal的學習管理系統soap basedweb服務並使用下面的代碼來使用它。但我無法知道如何捕捉來自JavaScript的服務響應,因爲我在這方面沒有太多工作。請幫助。使用javascript從總計認證Web服務獲得安全上下文令牌

<script language="JavaScript" type="text/javascript"> 

          function getData() 
          { 


    var xmlhttp = new XMLHttpRequest(); 
     xmlhttp.open('GET',  'https://Testsoapservice.com/Services/authentication.asmx?op=Login', true); 


      // build SOAP request 
      var sr = '<?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/">'+ 
        '<soapenv:Body>' + 
         ' <Login xmlns="http://www.testsoapservice.com/Authentication/">'+ 
         '<credentials>'+ 
'<Username>xxx</Username>'+ 
          '<Passcode>xxxx</Passcode>'+        
         '</credentials>'+ 
         '</Login>'+ 
        '</soapenv:Body>' + 
       '</soapenv:Envelope>'; 

      xmlhttp.onreadystatechange = function() { 


       if (xmlhttp.readyState == 4) {  

        if (xmlhttp.status == 200) { 
     // How to get the user token here as soap response. I would like to use the token to consume subsequent services 
        } 
       } 
      } 
      // Send the POST request 
      xmlhttp.setRequestHeader('Content-Type', 'text/xml'); 
      xmlhttp.send(sr); 


         } 
    </script> 

回答

0

這裏是答案:很簡單,但我嘗試不同的東西。這工作。

<script type="text/javascript"> 
      $(document).ready(function() { 
      $("#btnCallWebService").click(function (event) { 
      var wsUrl = 
"https://Testsoapservice.com/Services/authentication.asmx"; 

     xmlhttp.open('GET',  
'https://Testsoapservice.com/Services/authentication.asmx?op=Login', true); 


     // build SOAP request 
     var sr = '<?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/">'+ 
       '<soapenv:Body>' + 
        ' <Login 
    xmlns="http://www.testsoapservice.com/Authentication/">'+ 
        '<credentials>'+ 
'<Username>xxx</Username>'+ 
         '<Passcode>xxxx</Passcode>'+        
        '</credentials>'+ 
        '</Login>'+ 
       '</soapenv:Body>' + 
      '</soapenv:Envelope>'; 
      $.ajax({ 
       type: "POST", 
       url: wsUrl, 
       contentType: "text/xml", 
       dataType: "xml", 
       data: soapRequest, 
       success: processSuccess, 
       error: processError 
      }); 

     }); 
    }); 

    function processSuccess(data, status, req) 
    { 

     if (status == "success") 
      // $("#response").text($(req.responseXML).find("Value").text()); 
      var uToken=$(req.responseXML).find("Value").text(); 

}