在Axios中以GET方法URL搜索参数发送嵌套对象 [英] Send nested objects in GET method URL search params in Axios
问题描述
我正在尝试使用如下所示的URL搜索参数发送请求,但无法访问服务器端的嵌套对象 过滤器 .
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.
axios.get('/get handler', {
params: {
room: 1,
filter: {
fan: 2,
table: 1,
}
});
我可能做错了什么?
我在服务器端使用 Django restFramework 3,但无法访问该方法中的filter
键.我正在使用request.query_params
访问查询参数,但是当我执行request.query_params.get('filter')
时,我会得到none
What Am I possibly doing wrong?
I am using Django restFramework 3 on the server side and I am not able to access filter
key in the method. I am accessing query params using request.query_params
but when i do request.query_params.get('filter')
I get none
推荐答案
您需要序列化参数,并且可以通过编写一个小的配置来实现,如 此github问题 ,
You need to serialize your params and that you can do by writing a small config as mentioned in this github issue,
通常,您可以在应用程序的main.js文件或顶级文件中使用此配置,但这又取决于您何时执行该配置
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;
});
从axios 0.18.0开始:
// 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;
});
这篇关于在Axios中以GET方法URL搜索参数发送嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!