簡體   English   中英

Angular 5 Http api請求在chrome dev工具中得到響應,但不會返回

[英]Angular 5 Http api request gets response in chrome dev tools but won't return

我有一個“回復”內容:

在此輸入圖像描述

但我不能控制它.log它。

更新(15:00 22/03/2018,這是新版本):

在actions.component.ts中:

 generatePOR(){
    this._api.exportToERP(this.selection).subscribe((res) => {
      console.log('heelo I am second phase');
      console.log(res);
    }, (error) => console.log(error), () => {});
  }

在api.ts:

generatePOR (idList): any {
  const apiURL = `${this.API_URL}/purchaseorders/generatePOR`;
  return this._http.post(apiURL, idList, { headers: new HttpHeaders().set('Content-Type', 'application/json') });
}

這是控制台日志:

ro {headers: Ge, status: 200, statusText: "OK", url: "http://localhost:8080/purchaseorders/generatePOR", ok: false, …}error: {error: SyntaxError: Unexpected token P in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHttp…, text: "PARTNER_RELATION_CUSTOMER_GROUPCODE;PARTNER_RELATI…95;2;NEW ORDER PUBLISHED;;;2017-10-289 08:00:00
↵"}headers: Ge {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}message: "Http failure during parsing for http://localhost:8080/purchaseorders/generatePOR"name: "HttpErrorResponse"ok: falsestatus: 200statusText: "OK"url: "http://localhost:8080/purchaseorders/generatePOR"__proto__: eo

更新(16:31):

generatePOR (idList): any {
    const apiURL = `${this.API_URL}/purchaseorders/generatePOR`;
    this.PORresult = this._http.post(apiURL, idList, {
      observe: 'response',
      headers: new HttpHeaders({'Content-Type': 'application/json'}),
      responseType: 'text' as 'text'
    })
      .map((res, i) => {
        console.log('hi');
        console.log(res);
      });
    return this.PORresult;
  }

輸出:

hi  
ao {headers: Ge, status: 200, statusText: "OK", url: "http://localhost:8080/purchaseorders/generatePOR", ok: true, …}body: "PARTNER_RELATION_CUSTOMER_GROUPCODE;PARTNER_RELATION_CUSTOMER_PLANTCODE;PO_UpdateVersion;PARTNER_RELATION_SUPPLIER_NOLOCAL;PO_PoNumber;PO_PosNumber;PO_RequestNumber;PARTNER_RELATION_SUPPLIER_NO;PO_CollabPrice;PO_CollabPromQty;PO_Status;PO_SupAckNumber;PO_CollabComment;PO_CollabPromDate
↵PARTNER_RELATION_CUSTOMER_GROUPCODE;PARTNER_RELATION_CUSTOMER_PLANTCODE;1;PARTNER_RELATION_SUPPLIER_NOLOCAL;4500634818;00070;0001;PARTNER_RELATION_SUPPLIER_NO;464.95;2;NEW ORDER PUBLISHED;;;2017-10-289 08:00:00
↵"headers: Ge {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}ok: truestatus: 200statusText: "OK"type: 4url: "http://localhost:8080/purchaseorders/generatePOR"__proto__: eo

heelo I am second phase  undefined

使用.body來獲取你的回復

if (res) {
      if (res.status === 201) {
        return [{ status: res.status, json: res.body }]
      }
      else if (res.status === 200) {
        console.log('hello, I am a result ',res);
        return [{ status: res.status, json: res.body }]
      }
    }

你回歸承諾的方式在這里是錯誤的。

generatePOR (idList): Observable<any> {
const apiURL = `${this.API_URL}/purchaseorders/generatePOR`;
return this._http.post(apiURL, idList, { observe: 'response' }, )
  .map((res: HttpResponse<any>, i) => {
    if (res) {
      if (res.status === 201) {
        return [{ status: res.status, json: res }]
      }
      else if (res.status === 200) {
        console.log('hello, I am a result ',res);
        return [{ status: res.status, json: res }]
      }
    }
  })
  .catch((error: any) => {
    if (error.status < 400 ||  error.status ===500) {
      return Observable.throw(new Error(error.status));
    }
  })
  .pipe(
    catchError(this.handleError)
  );

}

不要退貨。結果,直接退還承諾。 因為api可能需要時間。 但在此之前,您將返回結果對象。 這就是你沒有得到這些數據的原因。

經過一天半的勞動,這對我有用:

actions.component.ts:

generatePOR(){
  this._api.generatePOR(this.selection).subscribe(res => {
    if(res !== null && res !== undefined){
      console.log(res.body);
    }
  }, (error) => console.log(error), () => {});
}

api.ts:

generatePOR(idList): any {
  const apiURL = `${this.API_URL}/purchaseorders/generatePOR`;
  this.PORresult = this._http.post(apiURL, idList, {
    observe: 'response',
    headers: new HttpHeaders({'Content-Type': 'application/json'}),
    responseType: 'text' as 'text'
  }).catch(this.handleError);
  return this.PORresult;
}

...並確保后端以text/csv格式發送實際文件,而不僅僅是粗暴的html

這個github線程幫助創建了正確的標題和選項: https//github.com/angular/angular/issues/18586

注意:你必須同時對api調用進行內聯,你不能事先在函數中或類或應用程序的其他地方將它們聲明為變量。 還有無意義的解析: responseType: 'text' as 'text'是使其工作的一個關鍵因素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM