Python 无法从flask api在react js中获得所需的响应
我正在使用Python 无法从flask api在react js中获得所需的响应,python,reactjs,flask,Python,Reactjs,Flask,我正在使用reactjs中的fetch库向我的flaskAPI获取数据,并从中推送数据。但无法从my api获得所需的响应 这是我的烧瓶api: @app.route('/adduser',methods=['POST']) def indx(): data=request.get_json(force=True) email=request.get_json()["email"] password=request.get_json()['password']
reactjs
中的fetch
库向我的flask
API获取数据,并从中推送数据。但无法从my api获得所需的响应
这是我的烧瓶api:
@app.route('/adduser',methods=['POST'])
def indx():
data=request.get_json(force=True)
email=request.get_json()["email"]
password=request.get_json()['password']
try:
auth.create_user_with_email_and_password(email,password)
except:
userexists="User Already Exists"
try:
user=auth.sign_in_with_email_and_password(email,password)
id = auth.get_account_info(user['idToken'])
db.child("users").push(id)
except:
invalidCredentials="Wrong Credentials"
if request.get_json(force=True):
x={
"name":"sarmad",
"roll":"052"
}
s=json.dumps(x)
return s
else:
return ""
这是反应js代码:
fetch('http://127.0.0.1:5000/adduser', {
mode:'no-cors',
method: 'POST',
headers: {
'Accept': 'application/json',
"Access-Control-Allow-Origin": "*",
'Content-Type': 'application/json'
},
body: JSON.stringify({
'email': this.state.email,
password: this.state.password,
name: this.state.name,
// userType: userTy,
dob:this.state.DOB,
address:this.state.Address,
gender:'male',
phone:'090078601',
// roles:roles
})
}).then((response) => response).then((responseJson) => {
console.log(responseJson);
//this.setState({pressed: false});
})
我需要以string
或json
的形式接收从Flask API传回的数据。以下是我目前的回复:
Response {type: "opaque", url: "", redirected: false, status: 0, ok: false, …} body: (...) bodyUsed: false headers: Headers {} ok: false redirected: false status: 0 statusText: "" type: "opaque" url: "" _proto_: Response
任何帮助都将不胜感激 只需使用
.json()
只需使用
.json()
响应{type:“不透明”,url:,重定向:false,状态:0,确定:false,}body:(…)bodyUsed:false headers:headers{}确定:false重定向:false status:0 statusText:“type:“不透明”url:_proto:u:Response这是我在浏览器的inspect选项中得到的响应不使用模式:'no cors'。见和。如果指定模式:“no cors”,浏览器将阻止前端JavaScript代码访问响应正文和响应标题。这就是“不透明”响应的含义。响应{type:“不透明”,url:,重定向:false,状态:0,ok:false,}body:(…)bodyUsed:false headers:headers{}ok:false重定向:false status:0 statusText:“type:“不透明”url:“\u proto:”响应这是我在浏览器的“检查”选项中得到的响应不使用模式:'no cors'。见和。如果指定模式:“no cors”,浏览器将阻止前端JavaScript代码访问响应正文和响应标题。这就是“不透明”响应的含义。请再次检查是否将
response.json()
放在括号内,因为您似乎将response.json
作为function.QA(@rana sarmad rajput)返回,此答案是否解决了您的问题?请再次检查是否将response.json()
放在括号内,因为您似乎将response.json
作为function.QA(@rana sarmad rajput)返回,这个答案解决了您的问题吗?
}).then((response) => response.json()).then((responseJson) => {
console.log(responseJson);
//this.setState({pressed: false});
})