[英]SyntaxError: JSON.parse: unexpected character on line 1 : Angular 5 Http call(Angular JS was working fine)
I have a Spring REST service which gives a byte array. 我有一个Spring REST服务,它提供了一个字节数组。 Previously I was using angularjs 1. Using FileSaver, I was able to download the pdf which is sent from REST service on the the response of a HTTP POST call.
以前,我使用angularjs1。使用FileSaver,我能够下载REST服务在HTTP POST调用的响应上发送的pdf。
Now, I am migrating my changes to angular 5. This is my HTTP post call in service - 现在,我将所做的更改迁移到angular5。这是服务中的HTTP post调用-
testPDF(abc: FormArray): Observable<HttpResponse<any>> {
const options = {
headers: new HttpHeaders({
'Content-type': 'application/json',
'response-type': 'blob'
})
}
return this.http.post<HttpResponse<any>>('/myRestPath', abc, options);
}
On my component typescript file I have subscribed like this - 在我的组件打字稿文件中,我这样订阅了-
xyz() {
this.myService.testPDF(this.abc.value).subscribe((file: any) => console.log('done'));
}
I have verified my REST service. 我已经验证了我的REST服务。 It returns proper byte array.
它返回正确的字节数组。 I have tried several other workarounds available on the internet.
我尝试了Internet上提供的其他几种解决方法。 But nothing seems to be working.
但是似乎没有任何作用。 In browser console I could see the response, which looks to be a PDF in byte stream.
在浏览器控制台中,我可以看到响应,它看起来像是字节流中的PDF。 In console I could see 3-4 MB size received, which make me believe that I have received the data but couldn't subscribe it.
在控制台中,我可以看到收到了3-4 MB的大小,这使我相信我已经收到了数据,但无法订阅它。 HTTP response code is also 200.
HTTP响应代码也是200。
It looks like by default, the response is tried to be fetched as a JSON instead of BLOB. 看起来默认情况下,尝试将响应作为JSON而不是BLOB来获取。 As my response doesn't start with '{', while parsing the data it couldn't parse the response.
由于我的响应不是以“ {”开头,因此在解析数据时无法解析响应。 I am not asking anyone to write code for me.
我不是要任何人为我编写代码。 For downloading PDF file also, I can do it using FileSaver.
也可以使用FileSaver来下载PDF文件。
The key problem I am facing is why my application is not entering into the subscribe block and execute the console statement. 我面临的关键问题是为什么我的应用程序没有进入订阅块并执行控制台语句。
Looks like you are setting the response type on the headers
, but HttpClient
expects it to be set on the responseType
property of the options
. 看起来您在
headers
上设置了响应类型,但是HttpClient
希望在options
的responseType
属性上进行设置。
For HttpClient
the following update would work 对于
HttpClient
,以下更新将起作用
testPDF(abc: FormArray): Observable<HttpResponse<Blob>> {
return this.http.post('/myRestPath', abc, {
observe: 'response',
responseType: 'blob'
});
}
A complete example which works for me with a GET
, provided there is a PDF file with the name some.pdf
under the assets
directory. 一个完整的示例,可以使用
GET
为我工作,前提是在assets
目录下有一个名为some.pdf
的PDF文件。
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class MyNewService {
constructor(private http: HttpClient) { }
getSomePDF(): Observable<HttpResponse<Blob>>{
return this.http.get('/assets/some.pdf', {
observe: 'response',
responseType: 'blob'
});
}
}
For older ones using @angular/http
, you can use the ResponseContentType
enum with an import { ResponseContentType } from '@angular/http';
对于使用
@angular/http
旧版本,可以将ResponseContentType
枚举与import { ResponseContentType } from '@angular/http';
一起使用import { ResponseContentType } from '@angular/http';
const options = {
responseType: ResponseContentType.Blob
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.