【问题标题】:Response with status: 200 OK for URL: null - Typescript Angular 2(SOAP Service call)响应状态:200 OK for URL:null - Typescript Angular 2(SOAP 服务调用)
【发布时间】:2017-03-15 13:32:08
【问题描述】:

对于使用我的 Nativescript 应用程序的 SOAP 服务调用,我得到的响应状态为“200”,但响应状态为“响应状态:200 OK for URL: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);
}

}

【问题讨论】:

  • 如果这是您的实际代码,那么alert('result ' + this.result); 不会等待结果从 SOAP 服务器返回(异步),对吧?

标签: angular typescript nativescript angular2-services angular2-nativescript


【解决方案1】:

您在subscribe 方法中放入的所有内容都会在您的服务器响应后异步执行。在您的情况下,您在填充之前使用 this.result。

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

【讨论】:

    猜你喜欢
    • 2017-07-06
    • 2017-06-16
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2013-03-24
    • 2017-10-29
    • 2017-04-07
    相关资源
    最近更新 更多