javascript - 使用 axios 时可能出现未处理的 Promise 拒绝、网络错误

标签 javascript reactjs react-native axios expo

我正在尝试使用 react-native、axios 和 Expo 发送 JSON 数据,但是当我在我的应用程序上按“发送”时,我收到以下警告:

Possible unhandled promise rejection, Error: Network Error



当我尝试通过 POSTman 发送通知 (JSON) 时,我的 API 正确接收通知 (JSON),但是当我尝试使用 axios 发送通知时,我收到上述警告消息,因此可能 react 没有正确发送数据。
export default class NotificationsInput extends React.Component {

state = {
    token: '',
    title: '',
    body: ''
}

handleToken = event => {
    this.setState({ token: event.target.value })
}

handleTitle = event => {
    this.setState({ title: event.target.value })
}

handleBody = event => {
    this.setState({ body: event.target.value })
}

handleSubmit = event => {
    event.preventDefault();

    let notification = JSON.stringify({
        token: this.state.token,
        title: this.state.title,
        body: this.state.body
    })

    let headers = {
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }

    }
    axios.post(`http://127.0.0.1:8000/send_push_message/`, notification, headers)
        .then(res => {
            console.log(res);
            console.log(res.data)
        })
        .then(error => console.log(error));

}

render() {
    return (
        <View>
            <TextInput onChange={this.handleToken}
                style={{ height: 25, width: 200, borderColor: 'black', borderWidth: 1 }}
            />
            <TextInput onChange={this.handleTitle}
                style={{ height: 40, width: 200, borderColor: 'black', borderWidth: 1 }}
            />
            <TextInput onChange={this.handleBody}
                style={{ height: 40, width: 200, borderColor: 'black', borderWidth: 1 }}
            />
            <Button onPress={this.handleSubmit} title="Send">Send</Button>
        </View>
    )
}

}

编辑 :

我添加了catch()函数,但现在的错误只是Network Error在控制台中。
handleSubmit = event => {
    event.preventDefault();

    let notification = JSON.stringify({
        token: this.state.token,
        title: this.state.title,
        body: this.state.body
    })

    let headers = {
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }

    }
    axios.post(`http://127.0.0.1:8000/send_push_message/`, notification, headers)
    .then(res => {
        console.log(res);
        console.log(res.data)
    })
    .catch(error => console.log(error));

}

最佳答案

我可以看到你已经链接了两个,.then 这是不正确的。您需要一个 catch block 来捕获网络错误。看看下面

  axios.post(`http://127.0.0.1:8000/send_push_message/`, notification, headers)
    .then(res => {
        console.log(res);
        console.log(res.data)
    })
    .catch(error => console.log(error));

关于javascript - 使用 axios 时可能出现未处理的 Promise 拒绝、网络错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52490497/

相关文章:

javascript - JS触摸轮播: performance issues

react-native - 无法连接到 Android 模拟器上的开发服务器

javascript - React Native panresponder 元素可点击?

reactjs - 单击更改状态,然后在延迟后再次更改并在 React 中显示消息

javascript - 如何根据单选按钮上选择的内容重置文本字段的值?

javascript - 在 React Native ScrollView 中将图像缩小到设备宽度

javascript - jquery防止父点击子函数

javascript - 特定条件下隐藏和打开div

javascript - 将传递的函数参数附加到 Controller 内的 $scope

css - 如何对齐 React Flexbox 中的文本?