简体   繁体   English

Angular 8 中的 Http post 响应问题

[英]Http post response issue in Angular 8

I am trying to get download the server response content as csv.我正在尝试将服务器响应内容下载为 csv。 While doing the same I have getting 2 errors.在做同样的事情时,我遇到了 2 个错误。

Method given below:方法如下:

generateCsv(exportModel: any) {
    let headers={};
    return this.http.post<any>(ReportConstant.exportApi, exportModel,
        { observe:"response",responseType: 'blob'})
} 

The error:错误:

ERROR in src/app/components/export/_services/export.service.ts(22,91): error TS2322: Type '"response"' is not assignable to type '"body"'. src/app/components/export/_services/export.service.ts(22,91) 中的错误:错误 TS2322:类型“响应”不能分配给类型“主体”。

src/app/components/export/_services/export.service.ts(22,110): error TS2322: Type '"blob"' is not assignable to type '"json"'. src/app/components/export/_services/export.service.ts(22,110): 错误 TS2322: 类型 '"blob"' 不可分配给类型 '"json"'。

I am very new with front end technologies.我对前端技术很陌生。 Kindly suggest me the solution to resolve the issue.请建议我解决问题的解决方案。

     return  this.httpClient.post(url,
            body,
            {
                headers: new HttpHeaders({
                    'Content-Type': 'application/json'
                }),
                observe: 'response',
                responseType: 'blob'
            }
        );

The way you are using the POST API(Http Client) is wrong您使用 POST API(Http Client) 的方式是错误的

If you are passing more data to server use POST or else GET should do the Job如果您将更多数据传递给服务器,请使用POST否则GET应该完成这项工作

POST: import statemenet in case you are doubtful POST:如果您有疑问,请导入statemenet

import {HttpClient, HttpHeaders, HttpParams, HttpParameterCodec} from "@angular/common/http";

add this in your constructor将此添加到您的构造函数中

private httpClient: HttpClient

Code starts:代码开始:

let body = new HttpParams({encoder: new CustomEncoder()});
body = body.append('args', 'stringArguments')); //Add needed arguments

//Set header and response types

let options= {
      headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'),
      responseType:'text' as 'json'
    };

// This will return a promise(observable) which can be called utilized as 
// observableReturn.subscribe(response => this.ProcessAsyncResponse(response), error => this.HandleSRProcessError(error, this.serviceRequestCallList));

return this.httpClient.post(ajaxUrl, body, options)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM