简体   繁体   English

为什么 axios get + post 请求在 5 个按钮请求后超时?

[英]why is axios get + post request timing out after 5 button requests?

I'm attempting to get and post data using axios from server (express, axios, react native) and after 5x pressing the button it disconnects from server.我正在尝试使用 axios 从服务器(快递,axios,本机反应)获取和发布数据,并在按下按钮 5 次后它与服务器断开连接。

please see web example - console log请参阅 web 示例 - 控制台日志

Is there a default timeout after 5 or 6 request calls - iOS is 3 calls before disconnect - android 5 calls before disconnect - web 6 calls - it varies across platforms.在 5 次或 6 次请求调用后是否存在默认超时 - iOS 是断开前 3 次调用 - android 断开前有 5 次调用 - web 6 次调用 - 它因平台而异。 Logs perfectly fine until disconnect.日志非常好,直到断开连接。

//client:
    const [i, setI] = useState(0)
    const [connecter, setConnecter] = useState()
    const mainURL = "http://192.168.0.26:8080/usr"
    const datas = {
        val : "counter!" + " " + i
    }
        const func = () => {
            setI(i + 1)

            axios({
            method: 'post',
            url: mainURL + "/play",
            data: JSON.stringify(datas),
            headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
            maxContentLength: Infinity,
            maxBodyLength: Infinity
            })
            .then(() => {
               console.log("sent!")
            })
            .catch(err => console.log(err))
            };

            const red = axios.get(mainURL + "/connect",
            {headers:{"Content-Type": "application/json"}})
            .then((res) => {
            setConnecter(res.status)  
            }).catch((err) => setConnecter(err))

            useEffect(() => {
            }, [red])
   
//server
router.get("/connect", (req, res) => {
    res.send("Server Connected")
})

router.post("/play", async(req, res) => {
    const resser = await req.body
    console.log(resser)
})

You can try using this code:您可以尝试使用以下代码:

import * as React from 'react';
import {useEffect, useState} from 'react';
import { Button, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import * as axios from "axios";

export default function App() {
const mainURL = "http://192.168.0.26:8080/usr";
const [counter, setCounter] = useState(0);
const [connecter, setConnecter] = useState(null);
 const [data, setData] = useState({
        val: `counter! ${counter}`
 });

 useEffect(() => {
    setData({
            val: `counter! ${counter}`
    });
 }, [counter]);

 const callApi = async () => {
   try {
      const connectRes = await axios.post('https://reqres.in/api/users', JSON.stringify(data), { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }});
   console.log(connectRes);
   setConnecter(connectRes);
   const result = await axios.get('https://reqres.in/api/products/3', { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }});
   setCounter(prev => prev + 1);
   console.log(counter);
   console.log(result);
   } catch (e) {
     console.log('error', e);
   }
 }
  return (
    <View style={styles.container}>
    <Button title="test" onPress={() => callApi()} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});

you can check this working example from here .你可以从这里查看这个工作示例。

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

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