我的响应状态为“200”,但响应为“状态响应:URL为200 OK”:对于使用Nativescript应用程序的SOAP服务调用,为null。我正在使用post方法,示例代码如下所示,
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);
}
}
答案 0 :(得分:1)
您在this.http.post(whatever_url, this.body, { headers: headers }).subscribe(
data => {
this.result = data;
alert(this.data); // should return something
},
err => this.logError(err),
() => console.log('Call complete')
);
alert(this.data); // <--- null, you hasn't received the server's response yet
方法中放入的所有内容都是在服务器响应后异步执行的。在你的情况下,你在填充之前使用this.result。
{{1}}