使用角度js将数据发布到SOAP api

Vik*_*kas 7 javascript soap web-services angularjs ionic-framework

我试图将数据发布到soap api但无法这样做.我已经尝试了所有可能的方法但仍然在调用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>
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情 -

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'}
        });
Run Code Online (Sandbox Code Playgroud)

这给出错误"无法处理请求.--->根元素丢失"

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;
Run Code Online (Sandbox Code Playgroud)

这是给出错误无法读取null的属性'getElementsByTagName'

有人可以帮我这个吗?几乎所有事情都没有运气.提前致谢

Tes*_*mas 5

  1. 使用$ http.post

您可以使用$ http服务将数据POST到SOAP API,如下所示:

在此输入图像描述

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
            });
Run Code Online (Sandbox Code Playgroud)
  1. 角肥皂

您还可以使用构建在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;
  });

})
Run Code Online (Sandbox Code Playgroud)