【发布时间】:2020-06-03 17:27:56
【问题描述】:
我尝试从 Angular 向我的 Laravel 后端发送 GET 请求。
Route::get('excel', function () {
return Excel::download(new PqrsExport, 'products.xlsx');
});
我订阅了这个方法
get_excel(){
this.pqrService.getExcel().subscribe(
resp => console.log(resp),
err => console.log(err)
);
}
得到这个
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8000/excel", ok: false, …}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
status: 200
statusText: "OK"
url: "http://localhost:8000/excel"
ok: false
name: "HttpErrorResponse"
message: "Http failure during parsing for http://localhost:8000/excel"
error: {error: SyntaxError: Unexpected token P in JSON at position 0 at JSON.parse (<anonymous>) at XMLHtt…, text: "PKVGSPG�D�X�[Content_Types].xml�…heets/_rels/sheet1.xml.relsPK��"}
__proto__: HttpResponseBase
【问题讨论】:
-
Angular 期望响应是 json 编码的,你应该返回 response()->json(Excel::download(new PqrsExport, 'products.xlsx')) 但我不认为它会工作像这样,你应该把pdf路径发送到angular。
-
我把
return response()->json(Excel::download(new PqrsExport, 'products.xlsx'))放在哪里? -
而不是返回 Excel::download(new PqrsExport, 'products.xlsx');
-
它现在没有显示错误,但它也没有下载任何东西
-
是的,这是正常行为,响应的内容是什么?
标签: angular laravel http http-get laravel-excel