【发布时间】:2019-06-23 18:10:47
【问题描述】:
我正在开始一个与 Flask 服务器交互的 Javascript 开发。当我提出请求(post 或 get)时,我总是遇到 405 method not allowed 错误。
这是我的 JavaScript 代码:
fetch('http://127.0.0.1:5053/api/prediction_lime').then(function (response) {
const contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
if (response.ok) {
response.json().then(function (json) {
app.showOneResult(0, json);
})
}
}
})
还有我的 Flask 的 Python 代码:
import flask
from flask import Flask, render_template, jsonify, request,send_file
from flask_cors import CORS, cross_origin
import numpy as np
import json
import os
#import pandas
print(flask.__version__)
app = Flask(__name__,template_folder='.')
CORS(app, origins="http://127.0.0.1:8080", allow_headers=[
"Content-Type", "Authorization", "Access-Control-Allow-Credentials"],
supports_credentials=True)
@app.route('/', methods=['POST'])
def index():
return render_template('index.html')
@app.route('/api/predictions',methods=['POST'])
def predictions():
# Cette fonction
params = request.get_json(force=True)
print(params)
with open('predictions.json', encoding='utf8') as f:
data = json.load(f)
return jsonify(data)
@app.route('/api/prediction_lime',methods=['POST'])
def prediction():
# Cette fonction
params = request.get_json(force=True)
print(params)
with open('prediction_lime.json', encoding='utf8') as f:
data = json.load(f)
return jsonify(data)
我已经阅读了很多论坛,但没有任何效果...
非常感谢。
【问题讨论】:
-
您的 JavaScript 代码缺少实际发送请求的组成部分。请添加。
-
这通常发生在您发送的 HTTP 方法不支持您请求的路由时。如果请求正确发送并发送到正确的路由,请检查 chome 开发者工具 > 网络
-
是的,发送的请求是正确的,它发布了json文件。
标签: javascript python-3.x flask http-status-code-405