Angular 6: How to set response type as text while making http call

后端 未结 6 1023
無奈伤痛
無奈伤痛 2020-11-28 09:05

I trying to make http request to the spring rest API.. API returns a string value (\"success\" or \"fail\")... but I dont know how to set the response type as string value w

6条回答
  •  無奈伤痛
    2020-11-28 09:57

    You should not use those headers, the headers determine what kind of type you are sending, and you are clearly sending an object, which means, JSON.

    Instead you should set the option responseType to text:

    addToCart(productId: number, quantity: number): Observable {
      const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
    
      return this.http.post(
        'http://localhost:8080/order/addtocart', 
        { dealerId: 13, createdBy: "-1", productId, quantity }, 
        { headers, responseType: 'text'}
      ).pipe(catchError(this.errorHandlerService.handleError));
    }
    

提交回复
热议问题