angular2调用soapweb服务失败,访问被拒绝



我正在尝试使用Angular2和HttpPOST对SOAP web服务进行一个非常简单的调用。如果我使用POSTMAN发布相同的消息,只需将内容类型设置为text/xml就可以了。

与Angular2我得到了这些错误:-

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

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

}

}

CORS头需要由处理请求的服务器添加到服务器的响应中。在客户端添加头不会有任何效果。

另请参阅访问控制不允许原点允许原点和https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

最新更新