1

I'm trying to get a really simple call to a SOAP web service to work using Angular2 and Http POST. if I post the same message using POSTMAN it works fine just by setting the content type to text/xml.

With Angular2 I'm getting these errors:-

SEC7120: Origin http://localhost:3004 not found in Access-Control-Allow-Origin header

SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied

any help would be appreciated.

this is the service code

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

}

}

user6231846
  • 105
  • 1
  • 4

1 Answers1

1

The CORS headers need to be added to the response of the server by the server you addressing the request to. Adding the headers on the client doesn't have any effect.

See also Origin is not allowed by Access-Control-Allow-Origin and https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567