You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Vue.js中GET请求查询传参方法?Axios POST转GET参数丢失问题

Fixing Axios GET Request Parameter Loss

Hey, I’ve run into this exact issue before when switching between POST and GET in Axios—let me walk you through the fix.

The core problem here is how Axios handles parameters for different request methods:

  • With axios.post(), you pass your data directly as the second argument (it gets sent in the request body).
  • With axios.get(), there’s no request body—instead, you need to package your parameters inside a params property within the configuration object (the second argument for get).

Here’s your modified code that will properly pass the filter parameters via GET:

export const filterDate = (options) => { 
  console.log(options) 
  // Replace post with get, and wrap options in a params object
  return axios.get(url, { params: options }).then(({ data }) => { 
    if (data.errors) throw new Error(JSON.stringify(data.errors)); 
    return data; 
  }) 
};

How this works

Axios automatically takes the options object under params and converts it into a query string appended to your API URL. For example, if your options are:

{ start: "2024-01-01", end: "2024-01-31", category: "news" }

Your request URL will become:
your-api-endpoint?start=2024-01-01&end=2024-01-31&category=news

This lets your PHP backend access the parameters using the $_GET superglobal (e.g., $_GET['start'], $_GET['end']) just like it would for any standard GET request.

Handling complex parameters (optional)

If your options include nested objects or arrays, Axios’s default serialization might not match what your PHP backend expects. In that case, you can customize the serialization with the paramsSerializer option (you’ll need the qs library for this):

import qs from 'qs';

export const filterDate = (options) => { 
  console.log(options) 
  return axios.get(url, {
    params: options,
    paramsSerializer: (params) => qs.stringify(params, { arrayFormat: 'brackets' })
  }).then(({ data }) => { 
    if (data.errors) throw new Error(JSON.stringify(data.errors)); 
    return data; 
  }) 
};

This ensures arrays and nested objects are formatted in a way PHP can parse correctly (e.g., filters[]=value1&filters[]=value2 for arrays).

内容的提问来源于stack exchange,提问作者Kotov Aleksandr

火山引擎 最新活动