对soap Web服务的angular2调用失败,访问被拒绝

时间:2016-05-09 12:29:28

标签: xml http post soap angular

我正在尝试使用Angular2和Http POST对SOAP Web服务进行非常简单的调用。如果我使用POSTMAN发布相同的消息,只需将内容类型设置为text / xml即可正常工作。

使用Angular2我遇到了这些错误: -

SEC7120:在Access-Control-Allow-Origin标头中找不到原点http://localhost:3004

SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝

任何帮助将不胜感激。

这是服务代码

import { Injectable } from 'angular2/core';
import { Http, Request, Response, Headers, RequestMethod, RequestOptions } from 'angular2/http';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class PaymentsService {

private body: string = `<?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>
<HelloWorld xmlns="http://bernera.zapto.org/" />
</soap:Body>
</soap:Envelope>`;

private result;

constructor(private http: Http) { }

callSOAP() {

var headers = new Headers();
headers.append('Content-Type', 'text/xml');
headers.append('Access-Control-Request-Method', 'POST');
headers.append('Access-Control-Request-Headers', 'X-Custom-Header');
headers.append('Access-Control-Allow-Origin', 'http://localhost:3004');

this.http.post('http://bernera.zapto.org/astronomy/astronomy.asmx',
  this.body,
  { headers: headers })
  .subscribe(
  data => this.result = data,
  err => this.logError(err),
  () => console.log('Call complete')
  );

alert('result ' + this.result);

}

logError(err) {
console.error('There was an error: ' + err.statusText);
alert('There was an error: ' + err.statusText);

}

}

1 个答案:

答案 0 :(得分:1)

您需要将请求的服务器的CORS标头添加到服务器的响应中。在客户端添加标题没有任何效果。

另请参阅Origin is not allowed by Access-Control-Allow-Originhttps://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS