首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >flask-cors没有解析来自ajax客户端的数据

flask-cors没有解析来自ajax客户端的数据
EN

Stack Overflow用户
提问于 2019-05-07 01:20:56
回答 1查看 60关注 0票数 0

我正在使用flask为区块链项目创建一些端点。我需要接受来自ajax客户端的json数据。因为是cros平台,所以我用的是flask cors。但我似乎找不到解决办法。它不起作用

我已经试过了

代码语言:javascript
复制
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app, origin = '*')

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

代码语言:javascript
复制
$.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);
        }
    });

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

代码语言:javascript
复制
@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

在服务器端,我没有接收到所需的数据,而在客户端,我得到了以下错误。

代码语言:javascript
复制
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.

请帮我解决这个问题。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-05-07 01:40:54

愚蠢的错误,使withCredentials: false

代码语言:javascript
复制
$.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);
        }
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56009740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档