烧瓶委员会不是从Ajax客户端解析数据



我正在使用烧瓶为区块链项目创建一些端点。我需要接受Ajax客户端的JSON数据。由于它是CROS平台,所以我使用烧瓶托架。但是我似乎找不到解决方案。它不起作用

我已经尝试做

from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app, origin = '*')

基本上我的客户端代码如下。

$.ajax({
        url: 'http://localhost:8080/ratings/new',
        dataType: 'json',
        type: 'POST',
        contentType: 'application/json',
        data: json1,
        crossDomain: true,
        xhrFields: {
            withCredentials: true
        },
        processData: false,
        beforeSend: function (xhr) {
            //xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
            xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
        },
        success: function (data, textStatus, jQxhr)
        {
            $('body').append(data);
            console.log(data);
        },
        error: function (jqXhr, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });

,在我的服务器上,我有一个

的终点
@app.route('/ratings/new', methods = ['POST','OPTIONS'])
def rating():
    values = request.get_json()
    if values == None:
        return "No data received", 400
    #ratings = values['rating']
    index = blockchain.new_ratings(values)
    response = {
        'Message': f'New transaction will be added to the block {index}',
        }
    response = jsonify(response)
    response.headers.add('Access-Control-Allow-Origin','*')
    return response, 201

在服务器端,我没有收到所需的数据,在客户端,我会遇到以下错误。

Access to XMLHttpRequest at 'http://localhost:8080/ratings/new' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

请帮助我解决这个问题。预先感谢。

愚蠢的错误,使credentials:false

$.ajax({
        url: 'http://localhost:8080/ratings/new',
        dataType: 'json',
        type: 'POST',
        contentType: 'application/json',
        data: json1,
        crossDomain: true,
        xhrFields: {
            withCredentials: false
        },
        processData: false,
        beforeSend: function (xhr) {
            //xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
            xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
        },
        success: function (data, textStatus, jQxhr)
        {
            $('body').append(data);
            console.log(data);
        },
        error: function (jqXhr, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });

相关内容

  • 没有找到相关文章

最新更新