我正尝试通过发布请求将Angular前端的b64编码的excel列表发送到我的Flask服务器,并让Flask服务器将b64编码的excel和日志文件返回到Angular前端。但是,即使从Flask收到200状态代码,我的浏览器中也会显示net :: ERR_EMPTY_RESPONSE。
下面是我在Angular和Flask中的实现:
角度服务
import { Injectable } from '@angular/core';
import { Observable, of, throwError } from '../../node_modules/rxjs';
import { catchError, map } from 'rxjs/operators';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
@Injectable({
providedIn: 'root'
})
export class PyMergeService {
url: string = 'http://127.0.0.1:5002/';
constructor(private http: HttpClient) { }
mergeRulebooks(data: Map<String, String>): Observable<any> {
return this.http.post<Response>(this.url + 'merge_xls', data, httpOptions).pipe(
map(this.extractData),
catchError(this.handleError)
);
}
private extractData(res: Response | any) {
let body = res.json();
return body || {};
}
private handleError(error: Response | any) {
let errMsg = (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
return of(errMsg);
}
}
角度组件
this.pyMergeService.mergeRulebooks(req).subscribe(
data => {
this.blobURL = this.b64toBlob(data['merge'], "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", null);
this.blobName = data['name'];
},
err => { console.log(err) }
);
烧瓶服务器
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
from xl_merge import get_merge
import json
import logging
app = Flask(__name__)
CORS(app, origins='http://localhost:4200', allow_credentials=True)
logging.getLogger('flask_cors').level = logging.DEBUG
@app.route('/merge_xls', methods=['POST'])
def merge_xls():
data = request.get_json()
res = get_merge(data)
return jsonify(res)
if __name__ == '__main__':
app.run(port='5002', threaded=True)