Ошибка CORS блокирует мой запрос POST, но позволяет выполнить запрос GET - PullRequest
0 голосов
/ 18 апреля 2020

Итак, я получаю сообщение об ошибке CORS, в котором говорится о политике CORS: в запрошенном ресурсе отсутствует заголовок «Access-Control-Allow-Origin». Дело в том, что в моем заголовке есть Access-Control-Allow-Origin. Я также импортировал CORS в свой python, и это помогло мне для запросов GET, но не для запросов POST.

Вот мой топор ios.

let URL = 'http://127.0.0.1:5000/Walls/saveComments'
       let HEADERS = {method: "POST","Content-Type": "application/x-www-form-urlencoded",'Access-Control-Allow-Origin': '*','Accept': 'application/json'}
       let data = {
         'post': post,
         'time': time,
         'comment': comment
       }

       console.log(this.state.postInfo)



       axios.post(URL, HEADERS, data)
         .then(function (response) {
           console.log(response);
           console.log(data)
         }).catch(function (error) {
          console.log(error);
        });
        }

Это мой бэкэнд. Я использую python для отправки данных в MongoDB.

from flask import Flask, Blueprint, jsonify, session
import json, requests, traceback
import pymongo
from flask_cors import CORS, cross_origin


from Authentication.views import Auth
from WallViews.views import Wall

app = Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
CORS(app)



app.secret_key = 'dsfdsfdsgfdgfdkjgrjk4k5j4k3j54'


app.register_blueprint(Auth)
app.register_blueprint(Wall)

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

Это мой маршрут в python.

@Wall.route('/saveComments', methods=['POST'])
@cross_origin()
def saveComments():
    header['Access-Control-Allow-Origin'] = '*'
    data = request.data     #{'comment':'Hey'}

    db = connectToDB()
    commentCollection = db.Comments
    commentCollection.insert_many(data)
    app.config['CORS_HEADERS'] = 'Content-Type'



    return jsonify('Saved Data!')
...