我在已部署到Heroku的应用程序中遇到了问题。当我在localhost上开发它时,没有收到此错误,但是一旦部署它,它就会自发地抛出此错误:
Access to fetch at
'https://frontend.herokuapp.com' from origin
'https://backend.herokuapp.com' has been blocked
by CORS policy: No 'Access-Control-Allow-Origin' header is present
on the requested resource. If an opaque response serves your needs,
set the request's mode to 'no-cors' to fetch the resource with CORS
disabled.
Flask服务器的简化版本:
from flask import Flask, request, make_response, jsonify, url_for,
redirect, render_template
from flask_cors import CORS, cross_origin
import flask
import json
app = Flask(__name__)
CORS(app)
@app.route('/api/action', methods=['GET', 'POST'])
@cross_origin()
def prescription(response={}):
# POST request
if request.method == 'POST':
print('score POST request')
response = request.get_json()
data = json.loads(response.get('data'))
rating = a_super_cool_feature
return jsonify(rating), 200
# GET request
else:
print('GET request')
return getScore(response)
@app.route('/api/score', methods=['GET', 'POST'])
@cross_origin()
def scoreHandler(response={}):
# POST request
if request.method == 'POST':
print('Incoming..')
response = request.get_json()
parseResponse(response)
return str(getScore(response)), 200
# GET request
else:
return getScore(response)
@app.route('/test')
def test_page():
# look inside `templates` and serve `index.html`
return render_template('index.html')
if __name__ == "__main__":
app.debug=True
app.run()
我的前端是一个React应用程序。这两个服务器在单独的Heroku服务器上运行,并且我使用的是免费版本,因此一次只能获得一个dyno(以防可能导致此错误)。
有趣的是:
大约有一半的时间,在已部署的Heroku应用程序上调用这些POST请求时,将引发错误。否则,它没有问题。有解决此问题的想法吗?
答案 0 :(得分:0)
您可以使用此代码段在cors应用程序范围内启用
。def create_base_network():
"""
Base network to be shared.
"""
model_input = Input(shape=(224,224,3))
x = Conv2D(64, (3, 3), activation='relu', padding='same')(model_input)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same')(x)
#x = MaxPooling2D(pool_size=(2,2))(x)
x = Dropout(0.5)(x)
#x = Flatten()(x)
x = Dense(1024, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(0.7)(x)
x = Dense(1024, activation='relu')(x)
x = Dense(1024, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(0.7)(x)
x = Dense(4096, activation='relu')(x)
x = Dense(4096, activation='relu')(x)
x = BatchNormalization()(x)
x = Dropout(0.7)(x)
# This layer is what the features are
x = Dense(4096, activation='relu')(x)
model = Model(inputs=model_input, outputs=x)
return model
答案 1 :(得分:0)
真的很奇怪的伪装修复。基本上,在向Flask后端发出POST请求时,大约有40%的时间会给我这个错误,然后我添加了
app.config['CORS_HEADERS'] = 'Content-Type'
在以下位置:
app = Flask(__name__)
CORS(app)
这并不能完全解决问题。然而;现在,仅在向后端提出请求时,才抛出此错误的时间约为5%。请参阅其他答案以获取实际修复。
答案 2 :(得分:0)
好的,现在我已经修复了它。我在Heroku中购买了“ Hobby”测功机,但现在我再也听不到错误。我认为这与我发出请求时另一台服务器无法正常运行有关,因为免费版本一次只能运行一个dyno。因此,它以某种方式超时并给出了该错误。
使用Hobby dyno类型,我的后端始终处于运行状态,因此当我发出请求时,它不再超时并给出该错误。