1

我将 ionic5 与 Angular 一起使用。我的 API 将 pdf 作为 Blob 对象返回。使用 Angular Http(适用于浏览器)时,我能够获得 Blob 响应

let headers =
  new HttpHeaders({
    'Accept': 'application/pdf',
    'Content-type': 'application/json',
    'Authorization': 'Bearer token',
    'Access-Control-Allow-Origin': 'localhost'
  });
  return this.httpClient.post(
         apiURL, 
         JSON.stringify(myBodyParamsObj), 
         {headers: headers, responseType: 'blob'}
     );

但是在使用离子原生 http 时我无法获得响应(由于使用 Angular Http 时的 CORS 问题,需要使用它在设备中运行应用程序)。尝试如下 -

   this.nativeHttp.setDataSerializer("raw"); // Tried other options like json etc. but none works
   this.nativeHttp.post(apiURL, params, { 'Authorization': myAuthHeaderObj, 'Content-Type': 'application/json',  'Accept': 'application/pdf', responseType: 'blob'});

也尝试过this.nativeHttp.sendRequest,但没有成功。

请帮忙

4

1 回答 1

1

最后它使用下面的代码工作 -

if(requestType == 'BLOB'){
      nativeHttpPromise =  this.nativeHttp.sendRequest(
        apiURL,
        {
          method: 'post',
          data: params,
          headers: { Authorization: authHeader },
          responseType: "blob",
          timeout: 5000
        }
      );
     }

所以基本上,nativeHttp.sendRequest() 不会使用 nativeHttp.post,而是会工作,因为它接受 responseType='blob'

于 2020-10-27T13:09:35.267 回答