xml - angular2 调用 soap web 服务失败,访问被拒绝

标签 xml http post soap angular

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

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

SEC7120:来源 http://localhost:3004在 Access-Control-Allow-Origin header 中找不到

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 header 添加到服务器的响应中。在客户端上添加 header 没有任何效果。

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

关于xml - angular2 调用 soap web 服务失败,访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37115865/

相关文章:

xml - 是否有一种 3D 格式是 SVG 的 3D 扩展?

c - Web 服务器 HTTP1.1 上的持久连接

xml - 在 XSLT 中为每个组选择不同的值

http - 如何使用 apache bench 提交带有 json 输入的 GET 请求?

java - 泽西客户端基本身份验证

http - 在 Netty Http 中对 Post 或 Get 的大请求

javascript - 尝试将数据从js发送到spring boot Controller

JAVA - 从网络服务器下载二进制文件(例如 PDF)文件

xml - 使用 sed(Windows + cygwin 和 Linux)更新 xml 属性?

sql-server - 选择 XML 多个仅具有相同名称的几个节点