Angular的Flask Python API CORS问题 [英] Flask python API CORS issue from Angular

查看:124
本文介绍了Angular的Flask Python API CORS问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Flask Python API,其cors设置如下。从浏览器调用时,它可以正常工作。但是从当前从localhost:4200开始测试的Angular应用程序调用时收到CORS错误。

I have Flask Python API with cors set up as below. It works fine when invoked from browser. But getting CORS error when called from Angular application currently testing from localhost:4200.

从Chrome开发人员工具中-可以看到预检请求,其中Request Method = Options(Preflight request)返回200,但实际请求抛出CORS错误。不知道我在做什么错。请分享见解。

From Chrome developer tools - Could see preflight request with Request Method=Options(Preflight request) returning 200, but actual request is throwing CORS error. Not sure what i am doing wrong here. Please share insights.

仅供参考-尝试过一些调整,例如将允许的来源设为 * ,删除 supports_credentials = True;仍然是相同的CORS错误

FYI - Tried couple of tweaks like making allowed origin "*" , removed "supports_credentials=True"; still same CORS error

错误:

Error:
Access to XMLHttpRequest at 'https://app01.dev.com/coreengine?rootfolder=app01&childfolder=jun2020&team=app01&action=customhierarchy' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

代码:

from flask_cors import CORS
mainapp = Flask(__name__)
cors = CORS(mainapp, resources={r"/*": {"origins":["http://localhost:4200"]}},supports_credentials=True)
@mainapp.route('/coreengine', methods=['GET','POST'])
def coreengine():
{
  #Code 
}


推荐答案

位于您的应用程序与package.json文件平行,创建一个新文件
proxy.config.json

At the root of your application parallel to package.json file, create a new file proxy.config.json

{
"/coreengine*": {
    "target":"https://app01.dev.manulife.com",
    "secure":true,
    "logLevel":"debug"
    }
}  

现在在脚本的package.json中:{},添加以下内容带有文件名proxy.config.json的亚麻来开始

Now in your package.json in scripts: {}, add the following flax with file name proxy.config.json to start"

{
"scripts": {
    "start":"ng serve --proxy-config proxy.config.json",
    "test":"ng test"
}
}

希望这对您有用。

这篇关于Angular的Flask Python API CORS问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆