2017-04-26 82 views
7

我試圖將數據發佈到soap api,但無法這樣做。 我已經嘗試了所有可能的方法,但仍然在調用API時出錯。將數據發佈到角度爲js的SOAP api

我的API是 - http://xyz.asmx?op=UserRegistration

,並以XML格式節選數據,如

<?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> 
    <UserRegistration xmlns="http://Service/"> 
     <Usercreditional>string</Usercreditional> 
    </UserRegistration> 
    </soap:Body> 
</soap:Envelope> 

的事情,我已經嘗試 -

1> $ http.post

var soapData = '<?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>'+ 
    '<UserRegistration xmlns="http://Service/">'+ 
    '<Usercreditional>[{ \'DeviceUUID\': \'' + data.DeviceUUID + '\', ' +   
         "\"DevicePushID\":\"" + data.DevicePushID + "\"}]" + 
        '</Usercreditional></UserRegistration></soap:Body></soap:Envelope>'; 

return $http({ 
      method: 'POST', 
      url: ' http://xyz.asmx?op=UserRegistration', 
      data : soapData, 
      headers : { "Content-Type" : 'application/xml'} 
     }); 

這給出錯誤「無法處理請求。 --- >缺少根元素」

2>的SOAPClient

var deferred = $q.defer(); 
var soapParams = new SOAPClientParameters(); 
var userCredentials = [{"DeviceUUID": data.DeviceUUID, "DevicePushID": data.DevicePushID}]; 
for (var param in userCredentials) 
    { 
     soapParams.add(param, soapParams[param]); 
    } 
var soapCallback = function (e) { 
    if (e.constructor.toString().indexOf("function Error()") != -1) { 
     deferred.reject(e); 
     } else { 
     deferred.resolve(e); 
      } 
     };  SOAPClient.invoke(' http://xyz.asmx', 'UserRegistration', soapParams, true, soapCallback); 
     return deferred.promise; 

這是給錯誤無法讀取空的特性‘的getElementsByTagName’

任何人都可以請幫我在這一個?試過幾乎所有的東西仍然沒有運氣。在此先感謝

回答

5
  1. 隨着$ http.post

可以使用$ http服務爲POST數據到SOAP的API如下:

enter image description here

var headers = { 
       'Content-Type': 'text/xml; charset=utf-8' 
      }; 
return $http({ 
       method: 'POST', 
       url: 'http://providerservice.fmcnetwork.net/MobileAppService.asmx?op=UserRegistration', 
       data : soapData, 
       headers : headers 
      }); 
  • 隨着angular-soap
  • 您還可以使用構建在SOAP客戶端頂部的插件angular-soap並使用$ soap服務實現一樣。

    例如:

    angular.module('myApp', ['angularSoap']) 
    
    .factory("testService", ['$soap',function($soap){ 
        var base_url = "http://www.cooldomain.com/SoapTest/webservicedemo.asmx"; 
    
        return { 
         CreateUser: function(firstName, lastName){ 
          return $soap.post(base_url,"CreateUser", {firstName: firstName, lastName: lastName}); 
         } 
        } 
    }]) 
    
    .controller('MainCtrl', function($scope, testService) { 
    
        testService.CreateUser($scope.firstName, $scope.lastName).then(function(response){ 
        $scope.response = response; 
        }); 
    
    }) 
    
    2

    試試這個:)從代碼

    var soapData = '<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">'+ 
        '<Body>'+ 
        '<UserRegistration xmlns="http://FmcMobileApplicationService/">'+ 
        '<Usercreditional>{ \'DeviceUUID\': \'' + data.DeviceUUID + '\', ' +   
             "\"DevicePushID\":\"" + data.DevicePushID + "\"}" + 
            '</Usercreditional></UserRegistration></Body></Envelope>'; 
    
    return $http({ 
          method: 'POST', 
          url: 'http://providerservice.fmcnetwork.net/MobileAppService.asmx' 
          data : soapData, 
          headers : { "Content-Type" : 'application/xml'} 
         }); 
    

    變化

    • 沒有發送XML頭
    • 更改SOAP:Envelope爲信封
    • 拿出模式命名空間
    • 更名皁:身體身體
    • 發佈到MobileAppService.asmx而不查詢字符串
    • 發送對象{ 「DeviceUUID」: 「1」, 「DevicePushID」: 「2」}不陣列

    我收到了回覆。這是一個空的一次,但一個真正的:) 我用1和2分別作爲DeviceUUID和DevicePushID。因此我承擔了一個空洞的迴應。

    響應:

    <?xml version="1.0" encoding="utf-8"?> 
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
        <soap:Body> 
         <UserRegistrationResponse xmlns="http://FmcMobileApplicationService/"> 
          <UserRegistrationResult>[]</UserRegistrationResult> 
         </UserRegistrationResponse> 
        </soap:Body> 
    </soap:Envelope>