首页 > 解决方案 > React、Axios 和 Flask - AttributeError: 'NoneType' 对象没有属性 'get'

问题描述

我正在尝试将我的react组件中的一些数据通过axios我的Flask服务器发布,如下所示:

handleSubmitSeeds(event) {
    event.preventDefault();
    const {userId} = this.props
    const data = {
      arabica: this.state.formSeeds
    };
    const url = `${process.env.REACT_APP_WEB_SERVICE_URL}/handle_seeds/${userId}`;
    axios.post(url, data)
      .then((res) => {
        console.log(data);
      })
      .catch((err) => {
      });
  };

在后端:

@seeds_bp.route('/handle_seeds/<user_id>', methods=['GET','POST'])
def handle_seeds(user_id):

    post_data = request.get_json()

    arabica = post_data.get('arabica')

    (...)

但我得到以下回溯:

AttributeError: 'NoneType' object has no attribute 'get'

我错过了什么?

标签: reactjsflaskaxios

解决方案


缺少带有 Content-Type授权承载的标头。一旦我这样做了:

var headers = {
          'Content-Type': 'application/json',
          Authorization: `Bearer ${window.localStorage.authToken}`
        }

并像这样通过它:

axios.post(url, data, headers)

request.get_json()工作。


推荐阅读