标头具有“访问控制允许来源:*”,但有角给我“阻止跨域请求:相同来源...”

时间:2019-03-13 15:05:25

标签: python angular docker cors flask-cors

很抱歉寻求帮助,但是我对angular和python api感到陌生,无法解决这个难题。

场景:我有两个Docker容器,在ubuntu环境中使用docker-compose进行了升级。第一个是pythonflask容器,它是api服务器,第二个是angular,它是angular应用。

我知道以下事实:由于CORS,我需要在我的python文件中安装flask-cors库,所以我做到了,一切似乎都还不错,因为当我只是简单地将“ curl -I http://pythonflask:5000/loan-requests”放在我得到的有角容器:

HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 41174

Access-Control-Allow-Origin: *

Server: Werkzeug/0.14.1 Python/3.7.2
Date: Wed, 13 Mar 2019 14:32:06 GMT

所以对我来说似乎很好,但是当我尝试从angular应用程序访问同一页面时,出现“跨域请求被阻止:相同来源策略不允许读取远程资源”错误,因此angular似乎没有无法获得正确的标题。

我已经阅读了问题Python Flask Cors Issue并尝试了其中的所有内容,所以现在对我来说,这似乎是一个角度问题,而不是服务器问题,原因是“ Access-Control-Allow-Origin:* “出现在标题中。

请清楚,这是docker-compose.yml文件和两个Dockerfile:

version: '2'

services:

    pythonflask:
        build: ./pythonflask
        stdin_open: true
        tty: true
        image: pythonflask
        ports:
            - "5000:5000"
        expose:
            - 5000

    angular:
        build: ./angular
        stdin_open: true
        tty: true
        image: angular
        ports:
            - "8080:80"
        expose:
            - 80
        volumes:
           - ./angular/dist:/usr/share/nginx/html 

    web:
        image: httpd  # this will use httpd:latest

Dockerfile pythonflask:


FROM python:stretch 

MAINTAINER XXXXX

RUN pip install flask \
    && pip install flask-restful \
    && pip install Flask-JWT \
    && pip install flask-cors \
    && pip install pycryptodomex \
    && pip install requests \
    && pip install python-dateutil

RUN apt-get update \
    && apt-get install -y curl \
    && apt-get install -y iputils-ping \
    && apt-get install -y nano

COPY src /src/

WORKDIR /src

EXPOSE 5000

ENTRYPOINT ["python", "/src/ethan_app.py"]

这是有角度的Dockerfile:


FROM nginx:latest

RUN apt-get update \
    && apt-get install -y curl \
    && apt-get install -y iputils-ping \
    && apt-get install -y nano

WORKDIR /usr/share/nginx/html

    COPY ./dist .

这是我用于python api的python代码段:


# coding: utf-8 

from flask import Flask
from flask_restful import Api
from flask_jwt import JWT
from flask_cors import CORS
import os

from ethan_loanRequest import LoanRequests

if not os.path.isfile("ethan.db"):
    resetDemo = ResetDemo()
    resetDemo.resetDemo()

UPLOAD_FOLDER = './uploads'
app = Flask(__name__)
app.secret_key = "xxxxx"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
CORS(app, resources={r"/*": {"origins": "*"}})
api = Api(app)
jwt = JWT(app, authenticate, identity) # /auth
api.add_resource(LoanRequests, '/loan-requests')
if __name__ == '__main__':
    app.wsgi_app = LoggingMiddleware(app.wsgi_app)    
    app.run(port=5000, debug=True, host='0.0.0.0')

这是与角度7相关的前端APP部分:


      loanRequestsGet(queryParams: {}) {
        const headers = new Headers({
          'Content-Type': 'application/json'
        });
        return this.http.post(this.httpLoanRequests,
                            queryParams,
                            {'headers': headers});
      }

在此先感谢大家渴望帮助我。

0 个答案:

没有答案