Creating a SOAP client with angular 2 [closed]

拥有回忆 提交于 2019-12-01 19:18:28

What you need is a service that wraps around Http and provides deserialization:

@Injectable()
export class SOAPService{

    constructor(private http:Http){}

    public get(url:string, options?:RequestOptionsArgs):Observable<any>{
        return this.http.get(url, options).map(res => {
            let xmlresult = res.text();
            let result = //Here you deserialize your xml object
            return result;
        }
    }
}

Then you can use it this way:

@Component({...})
export class ExampleComponent{
    constructor(private soap:SOAPService){}


    foo():{
        this.soap.get('foo/bar').subscribe(...);
    }
}

Since I'm not an xml parsing expert, I can't tell you how to deserialize your XML, but you can check MDN for that, you simply have to wrap the serialization/deserialization process in another service and inject it in your SOAPService.

You could use a regular http-request with Angular2's [Http class] (https://angular.io/docs/ts/latest/api/http/index/Http-class.html) This is an example from their page:

import {Http, HTTP_PROVIDERS} from '@angular/http';
import 'rxjs/add/operator/map'
@Component({
  selector: 'http-app',
  viewProviders: [HTTP_PROVIDERS],
  templateUrl: 'people.html'
})
class PeopleComponent {
  constructor(http: Http) {
    http.get('people.json')
      // Call map on the response observable to get the parsed people object
      .map(res => res.json())
      // Subscribe to the observable to get the parsed people object and attach it to the
      // component
      .subscribe(people => this.people = people);
  }
}

instead of asking for a json file, you could use a URL instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!