Send nested objects in GET method URL search params in Axios

前端 未结 1 1119
时光说笑
时光说笑 2021-01-22 21:02

I am trying to send the request with URL search params as below but I am not able to access a nested object filter on the server side.

         


        
1条回答
  •  走了就别回头了
    2021-01-22 21:12

    You need to serialize your params and that you can do by writing a small config as mentioned in this github issue,

    Usually you would have this config in the main.js file or the top level file of your application, but again it depends on when you want to execute it

    // main.js
    import axios from "axios";
    
    // Format nested params correctly
    axios.interceptors.request.use(config => {
      window.console.log(config);
    
      config.paramsSerializer = params => {
        // Qs is already included in the Axios package
        return Qs.stringify(params, {
          arrayFormat: "brackets",
          encode: false
        });
      };
    
      return config;
    });
    

    From axios 0.18.0 onwards:

    // main.js
    import axios from "axios";
    import Qs from 'qs';
    
    // Format nested params correctly
    axios.interceptors.request.use(config => {
    
      config.paramsSerializer = params => {
        // Qs is not included in the Axios package
        return Qs.stringify(params, {
          arrayFormat: "brackets",
          encode: false
        });
      };
    
      return config;
    });
    

    0 讨论(0)
提交回复
热议问题