我正在尝试从Angular前端向Python Flask后端发出http get请求。
Python如下:
app = Flask(__name__)
CORS(app)
app.config['SECRET_KEY'] ='121212'
app.config['CORS_ALLOW_HEADERS'] = "Content-Type"
app.config['CORS_HEADERS'] = 'Content-Type'
logging.getLogger('flask_cors').level = logging.DEBUG
...
def get_news():
query = request.args.get('query')
sort = request.args.get('sort')
results = newsapi.get_everything(q=query,language='en', page_size=100, sort_by=sort)
format(results["articles"])
articles = results["articles"]
num = results["totalResults"]
print("found "+str(num))
for article in articles:
if not isInMongo({"description":article["description"]}):
print("adding")
news.insert_one(article)
return make_response(jsonify(articles), 200,header="Access-Control-Allow-Origin")
TypeScript如下:
search(query,sort){
console.log('searching')
return this.http.get('http://localhost:5000/api/v1.0/getNews/?query='+query+"&sort="+sort).subscribe(response=>{
this.getNews(this.current_page);
});
}
控制台输出为: console output
所有其他http请求都正常运行,从邮递员发送时出现问题的http请求也是如此。
我已经在stackoverflow上找到了几种解决方案,但是很多答案都是“您必须在请求上将Access-Control-Allow-Origin设置在服务器上”的答案……说实话,我没有了解如何做到这一点。