处理React应用上Axios请求中的425 Too Early错误

fiei3ece  于 5个月前  发布在  iOS
关注(0)|答案(1)|浏览(89)

在第一篇文章中,我试图用axios调用服务器,它对我很有效。第二篇文章也是如此。
然后在第二个Get中,我得到错误425(太早)。
代码崩溃了。
我该怎么补救?

const handleUpload = async () => {
    setIsLoading(true);
    if (selectedFile && selectedLanguage) {
      const formData = new FormData();
      formData.append("file", selectedFile);
      formData.append("language", String(selectedLanguage?.code));
  
      try {
        const baseURL = process.env.REACT_APP_API_URL;
  
        // First API call to upload the file
        const uploadResponse = await axios.post(
          `${baseURL}/upload/`,
          formData,
          {
            headers: {
              "Content-Type": "multipart/form-data",
            },
          }
        );
  
        const newId = uploadResponse.data.public_id;
        setSelectedId(newId);
  
        // Second API call to add translation
        const addTranslation = await axios.post(
          `${baseURL}/upload/sources/${newId}/outputs`,
          { language: selectedLanguage.code }
        );
        const newOutputId = addTranslation.data.id;
  
        // Third API call to get the translated content
        try {
          debugger;
          const getTranslation = await axios.get(
            `${baseURL}/upload/outputs/${newOutputId}/contents/`
          );
          const newVideoUrl = getTranslation.data.url;
          setVideoUrl(newVideoUrl);
        } catch (error) {
          console.error("Error fetching translation:", error);
          // Handle error specific to fetching translation
        }
      } catch (error) {
        console.error("File upload failed:", error);
        // Handle general upload error
      } finally {
        setIsLoading(false);
      }
    }
  };

字符串
我试着使用settimeout,分割功能,分割成不同的文件.它没有帮助我。

3gtaxfhh

3gtaxfhh1#

由于没有人回答这个问题。我对425网络通信安全性很好奇。
我不知道这是否有用,但值得一试。
如果你在延迟完成后自动重试请求,仍然得到425错误,并继续等待直到响应完成,怎么样?

try
 {
    const getTranslation = await 
    retryOn425Error(`${baseURL}/upload/outputs/${newOutputId}/contents/`);
    // rest of the code
  }

字符串

这里是retry函数

// change the retries value 
async function retryOn425Error(url, retries = 10, delay = 1000) {
  try {
    return await axios.get(url);
  } catch (error) {
    if (retries > 0 && error.response && error.response.status === 425) {
      await new Promise(resolve => setTimeout(resolve, delay));
      return safeGetRequest(url, retries - 1, delay * 2);
    }
    throw error;
  }
}


如果您一直收到425错误,那么您可能需要查看服务器日志以查看错误和安全配置

相关问题