繁体   English   中英

无法从flask API在react js中获得所需的响应

[英]Cant get desired response in react js from flask api

我现在用的是fetch库从reactjs获取和从我的将数据推送到/ flask API。 但是无法从我的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 ""

这是React 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});
})

我需要接收从Flask API传回的数据,无论是string还是json 这是我目前的回复:

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()

}).then((response) => response.json()).then((responseJson) => {
  console.log(responseJson);
 //this.setState({pressed: false});
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM