【问题标题】:Creating a SOAP client with angular 2 [closed]使用 Angular 2 创建 SOAP 客户端 [关闭]
【发布时间】:2017-02-14 21:17:55
【问题描述】:

我正在寻找一种使用 WSDL 将 SOAP 请求发送到 Web 服务的方法。是否可以使用 Typescript 2 和 Angular 2 做到这一点?

我看过 Angular 1 的教程,但他们使用旧的 Angular 方法,例如 factorycontroller

如果可能的话,我想要一种使用 TypeScript 的新方法。

有什么想法吗?

【问题讨论】:

  • 这个运气好吗?如果您可以使用,请分享您的解决方案。

标签: soap angular typescript ionic-framework soap-client


【解决方案1】:

您需要的是一个围绕Http 并提供反序列化的服务:

@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;
        }
    }
}

那么你可以这样使用它:

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


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

由于我不是 xml 解析专家,我无法告诉您如何反序列化您的 XML,但您可以查看MDN,您只需将序列化/反序列化过程包装在另一个服务中并注入它在你的SOAPService

【讨论】:

  • 如果我错了,请纠正我,但这与在 Angular2 的 Http 类周围制作自己的包装器一样,对吧?您还不如直接使用 Http 类?
  • 这是 Angular2 的 Http 类的包装器;)。当然你可以直接使用Http,但问题是它会涉及很多重复的代码(在你需要SOAP的地方,你会重复反序列化逻辑)。
  • 好的,我正在尝试您的回复,但我不明白如何从我的应用组件中提供RequestOptionsArgs 参数?我该如何申报? subscribe 的作用是什么?在此先感谢:)
  • options 参数是可选的,就像Http 一样,除非您希望在请求中包含一些特定选项(特定标头、内容类型等),否则您不必提供它. subscribe 函数允许你连接 Http 的异步行为,就像你传递了一个回调来处理最终接收到的数据。
  • @Supamiu 好的,谢谢,如果我想调用在我的 Web 服务上运行的特定函数,我在哪里可以指定它? :)
【解决方案2】:

您可以使用带有 Angular2 的 [Http class] (https://angular.io/docs/ts/latest/api/http/index/Http-class.html) 的常规 http 请求 这是他们页面中的一个示例:

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);
  }
}

您可以使用 URL,而不是请求 json 文件。

【讨论】:

  • SOAP 使用 XMl 而不是 json,这就是问题所在:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多