简体   繁体   English

Axios POST 方法:405 方法不允许

[英]Axios POST Method: 405 Method Not Allowed

I'm building a webapp with React and Flask and I have an issue with POST request.我正在使用 React 和 Flask 构建一个 webapp,但我遇到了 POST 请求的问题。

This is my app.py file:这是我的app.py文件:

import sys
import os

from flask import Flask, jsonify, request, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS, cross_origin
from flask_mail import Mail, Message

from models import User, Project, Image, db
from api import blueprints
from tools import format_email


app = Flask(__name__,
  static_folder='../build/static',
  template_folder="../build"
)

app.config.from_object(os.environ['APP_SETTINGS'])
db.init_app(app)
cors = CORS(app)
mail = Mail(app)

# Register the blueprints
for b in blueprints:
  app.register_blueprint(b)

@cross_origin
@app.route('/', defaults={'u_path': ''})
@app.route('/<path:u_path>')
def index(u_path=None):
  return render_template("index.html")


@app.route('/api/process_email', methods=['POST'])
def process_email():
  print('plop')
  data = request.get_json()
  formated_email = format_email(
    data['firstname'],
    data['lastname'],
    data['email'],
    data['message']
  )
  msg = Message(formated_email['title'], sender='plop@gmail.com', recipients=['plop@gmail.com'])
  msg.body = formated_email['textbody']
  msg.html = formated_email['htmlbody']
  mail.send(msg)

  return 'done'

if __name__ == "__main__":
  app.run()

In the config.py I have this set CORS_HEADERS = 'Content-Type'config.py我有这个设置CORS_HEADERS = 'Content-Type'

When I test this with Postman, my email is sent without any issue.当我用 Postman 测试这个时,我的 email 发送没有任何问题。 But from the app, I get a 405 METHOD NOT ALLOWED response.但是从应用程序中,我得到了405 METHOD NOT ALLOWED响应。

This is how I send the request:这就是我发送请求的方式:

axios.post(API_URL + 'process_email/', {
      "firstname": values.firstname,
      "lastname": values.lastname,
      "email": values.email,
      "message": values.message
    }, {
      headers: {
        'Content-Type': 'text/plain;charset=utf-8',
      },
      withCredentials: 'same-origin'
    })
    .then(({ data }) => {
          console.log(data)
      }, (error) => {
        toast.error("gneuuuu");
      })
      .catch(() => {toast.error("Oups! Something went wrong!")});

This is what I have:这就是我所拥有的:

在此处输入图像描述

Since I put a proxy in place, I don't see preflights request anymore.由于我设置了代理,我不再看到预检请求。 I tried with a simple fetch or use superagent but issue is still here, and I clearly don't understand how CORS works.我尝试使用简单的fetch或使用superagent ,但问题仍然存在,我显然不明白 CORS 是如何工作的。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks!谢谢!

If anyone needs the answer: issue was in the way I was sending the request with axios.如果有人需要答案:问题在于我使用 axios 发送请求的方式。 Instead of:代替:

axios.post(API_URL + 'process_email/', {...

I have to remove the trailing /: This works fine:我必须删除尾随/:这很好用:

axios.post(API_URL + 'process_email', {...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM