Creating a SOAP client with angular 2 [closed]

2019-02-23 15:57发布

I'm looking for a way to send SOAP request to a web service, with a WSDL. Is it possible to do that with Typescript 2 and Angular 2 ?

I've seen tutorials for Angular 1 but they used old angular methodes, like factory or controller.

I would like if it's possible, a new way to do that with TypeScript.

Any ideas ?

2条回答
放我归山
2楼-- · 2019-02-23 16:35

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.

查看更多
甜甜的少女心
3楼-- · 2019-02-23 16:52

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.

查看更多
登录 后发表回答