访问ReactJS中POST API请求返回的JSON响应

时间:2018-06-05 10:48:45

标签: json reactjs api post jsonresponse

我正在ReactJS中构建注册页面。表单收集的数据通过POST方法发布到API,该API返回JSON响应,如下所示,用于有效输入

{"message":"Registration Success","status":true}

我想访问此JSON响应的组件以检查消息和状态。我使用的代码如下

onSubmitSignIn = () => {
    fetch(' https://example.com/v1/users/register', {
       method: 'post',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
        },
        body: JSON.stringify({
            email: this.state.email,
            password: this.state.password,
            firstName: this.state.firstName,
            lastName: this.state.lastName,
            mobile: this.state.mobile
        })
    })
        .then(response => {
            console.log('sss', response);
            return response.json();
        })
}

但是这会在Unexpected end of input行返回错误.then(response => ...。任何人都可以帮我解决如何正确编写访问和检查API响应的方法吗?

0 个答案:

没有答案