【发布时间】:2019-03-01 12:26:02
【问题描述】:
我在 ReactJS 中使用 axios 提出这个 API 请求
axios.post(`${API_URL}/valida_proximo`, {
id: images.map(image => image.id)
},
getAxiosConfig())
// this.setState({ images, loadingAtribuiImagens: false})
}
它在 Google Chrome 中运行良好,但在 Firefox 上我收到一个错误:
跨域请求被阻止:同源策略不允许读取位于http://localhost:5000/valida_proximo 的远程资源。 (原因:CORS 请求没有成功)。[了解更多]
我能做什么?
这是我的 API
@blueprint.route('', methods=['POST', ])
@jwt_required()
def index():
if request.json:
id_usuarioImagem = request.json.get('id')
imagens_selecionadas =
UsuarioImagem.query.filter(UsuarioImagem.id.in_(id_usuarioImagem)).all()
if imagens_selecionadas:
for imagem_selecionada in imagens_selecionadas:
imagem_selecionada.batido=True
db.session.commit()
return 'ok', 200
return 'error', 400
【问题讨论】:
-
axios.post 支持 CORS 吗? github.com/axios/axios/issues/1358 使用
fetch(),我知道我需要将'credentials': 'same-origin'添加到 args 以使 CORS 工作。但是,我找不到 axios 的等效选项。 CORS 还有很多其他问题可能会出错,但是,这就是我要开始的地方。 -
如果您的应用在安全上下文 (https) 上运行,这可能与以下 Firefox 错误有关:bugzilla.mozilla.org/show_bug.cgi?id=1376310