代码之家  ›  专栏  ›  技术社区  ›  hwkal

“为什么我的Axios请求在包含'populate=*'查询参数时返回404 Not Found错误?”

  •  0
  • hwkal  · 技术社区  · 2 年前
    
    **Home.jsx**
    
    import { FetchDataFromApi } from "../../utils/api";
    import { useEffect } from "react";
    
    const Home = () => {
      useEffect(()=>{
        getCategory();
      })
      const getCategory = ()=>{
        FetchDataFromApi("/api/categories/populate=*").then((res)=>{
          console.log(res);
        })
      }
    return (...)}
    export default Home
    
    
    ------------------------------------------------------------------
    
    **api.js**
    
    import axios from "axios";
    
    const params = {
        headers:{
            Authorization: "bearer " + "myApiToken"
        }
    }
    
    export const FetchDataFromApi = async(url)=>{
        try{
            const {data} = await axios.get("http://localhost:1337" + url, params)
        return data;
        }catch (err){
            console.log(err);
            return err;
        }
    }
    
    
    

    我正在尝试在我的React应用程序中使用Axios向API端点发出GET请求。当我在URL中包含查询参数“populate=*”时,我总是收到一个404 Not Found错误。然而,当我删除此参数时,请求工作正常,但我无法获得图像的详细信息。不过,我正在遵循某人的代码。

    错误-“加载资源失败:服务器响应状态为404(未找到):1337/api/categies/popuple=*”

    1 回复  |  直到 2 年前
        1
  •  1
  •   Darko    2 年前

    查询参数由分隔 ? 符号,不是 / 。所以你需要写下以下内容。

        FetchDataFromApi("/api/categories?populate=*").then((res)=>{
          console.log(res);
        })
    

    “如果您的请求有许多查询参数,则必须将它们与 & 象征您可以在脑海中确定查询参数的顺序。

        FetchDataFromApi("/api/categories?populate=*&city=abc&other=other").then((res)=>{
          console.log(res);
        })