我尝试使用以下代码发送数据:
let data = new FormData();
data.append("username", userName);
console.log(data)
var fetchOptions = {
method: 'POST',
headers: {
"Accept": "application/json",
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'mode': 'no-cors',
'cache': 'no-cache'
},
body: data
}
fetch(url, fetchOptions)
.then((response) => {
if (response.ok) {
responseJson = response.json();
console.log(responseJson, response.status);
if (response.status == 200) {
alert('success!');
this.state.code = false;
}
}
})
.catch((error) => {
console.error(error);
});
在 flask 中我收到了这些数据
username = request.form['username']
return jsonify({'data': 'Code received!'})
但这没有用。它返回以下内容 console.log(responseJson, response.status);
Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
} 200
知道为什么会发生这种情况吗?
最佳答案
response.json() 返回一个 promise 并应该得到解决。
引用https://developer.mozilla.org/en-US/docs/Web/API/Body/json
如果你这样做 .then(response => response.json()).then(json => console.log(json))
你应该看到你想看到的内容
关于javascript - 无法将数据从 React Native 发送到 Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45118784/