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

时间:2019-02-08 14:56:57

标签: python reactjs flask

我正在使用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 ""

这是反应式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传回的数据,形式为stringjson。这是我目前的回复:

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

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

只需使用.json()

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