reactjs - 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'
我错过了什么?
解决方案
缺少带有 Content-Type和授权承载的标头。一旦我这样做了:
var headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${window.localStorage.authToken}`
}
并像这样通过它:
axios.post(url, data, headers)
request.get_json()
工作。
推荐阅读
- javascript - 目的是什么 usage in Vuetify?
I want to use Vuetify 2.0 in my project and currently reading about v-stepper component which is used to display progress throu
- flutter - _AssertionError('package:firebase_auth/src/firebase_auth.dart':断言失败:第 95 行 pos 12:'email != null':不正确。)
- javascript - 当另一个字段的值等于某个值时,我想将一个表单字段替换为另一个
- html - 如何在 HTML 和 CSS 中设置交替的 div?
- f5 - Big-IP F5 irule 更改 uri
- javascript - 如何使用 Highcharts 构建彩色树状图
- ruby-on-rails - Rails Webpacker SCRIPT5009:'app' 未定义 - IE11 问题
- c# - FindViewByID 和 Xamarin.Forms.Listview 不起作用
- python - 在不知道名称的情况下扫描嵌套的 json 响应以获取特定值
- google-cloud-storage - 尝试使用 Deployment Manager 创建存储桶时禁止 403