Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用axios时可能出现未经处理的承诺拒绝、网络错误_Javascript_Reactjs_React Native_Axios_Expo - Fatal编程技术网

Javascript 使用axios时可能出现未经处理的承诺拒绝、网络错误

Javascript 使用axios时可能出现未经处理的承诺拒绝、网络错误,javascript,reactjs,react-native,axios,expo,Javascript,Reactjs,React Native,Axios,Expo,我尝试使用react native、axios和Expo发送JSON数据,但当我在应用程序上按“发送”时,我收到以下警告: 可能未处理的承诺拒绝,错误:网络错误 当我尝试通过POSTman发送通知时,我的API正确地接收到了通知(JSON),但是当我尝试使用axios发送时,我收到了上面的警告消息,因此可能react没有正确地发送数据 export default class NotificationsInput extends React.Component { state = {

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

可能未处理的承诺拒绝,错误:网络错误

当我尝试通过POSTman发送通知时,我的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而不是then进行错误处理

.catch(error => console.log(error));

我可以看出你用链子锁了两个,这是不正确的。您需要一个catch块来捕获网络错误。看看下面

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

我尝试过,但在后端启用的控制台上不断出现相同的错误?您收到的警告消息是什么可能未经处理的承诺拒绝(id:0):错误:网络错误您的错误表明您没有处理承诺拒绝,即添加
。其他用户提到的catch
,也请检查此项:添加
后。catch
阻止您收到的错误是什么?仅网络error@Rik我是说在他的代码里。他试图捕捉错误。然后我尝试添加一个捕捉,但这只会使错误突然出现在我的脑海中console@BielBorba这是正确的,这意味着您的呼叫被后端拒绝。我认为它在有效载荷中寻找一些它没有发现和失败的数据。
  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));