Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get请求时 传键值对无法解析 #369

Open
ByteXiong opened this issue May 26, 2024 · 11 comments
Open

Get请求时 传键值对无法解析 #369

ByteXiong opened this issue May 26, 2024 · 11 comments
Labels
bug:confirmed Something isn't working good first issue Good for newcomers Vue Issues or pull request for Vue

Comments

@ByteXiong
Copy link

ByteXiong commented May 26, 2024

请详细描述bug

get传参{ keyWord:"", sortList: { sort: 'desc' }}
在Axios中 get url: ?keyWord=&sortList[sort]=desc
在alova中 get url: ?keyWord=&sortList=[object%20Object]

复现链接

No response

复现步骤

get请求传 json 键值对

期望的表现

No response

alova 版本

2.20.5

设备/浏览器/浏览器版本

No response

额外的说明

No response

@ByteXiong ByteXiong added bug:need-confirm It's probably a bug Vue Issues or pull request for Vue labels May 26, 2024
@MeetinaXD
Copy link
Contributor

你好,请提供部分可复现的代码。

@ByteXiong
Copy link
Author

const list = ref([]);
const param = reactive({
pageIndex: 1, //当前页
pageSize: 10, //当前显示
sortList: { sort: 'desc' },
});
const getData = async () => {
let { data } = await get('/api/UserAddress/getMyPage', param, loading);
list.value.push(...data.data);
};

@MeetinaXD
Copy link
Contributor

这看着不是 alova 的用法,你是不是在用 axios 或者其他的库?

@ByteXiong
Copy link
Author

export const get = <T = any>(url: string, params?: T, loading?: Ref) => {
if (loading) loading.value = true;
return useRequest(request.Get(url, { params: params || {} }))
.send()
.finally(() => {
if (loading) loading.value = false;
});
};
我底层封装过, 你get 传一个键值对参数{sortList: { sort: 'desc' }}就能找到问题

@ByteXiong
Copy link
Author

我想解决的问题是 在 http query里怎么才能传键值对

@MeetinaXD
Copy link
Contributor

了解了,原来你说的是嵌套对象下的序列化问题。

这个问题我们复现了。目前你暂时可以通过 qs 来手动序列化 params。

问题修复后我们会在这里回复 :)

@MeetinaXD MeetinaXD added bug:confirmed Something isn't working and removed bug:need-confirm It's probably a bug labels May 27, 2024
@LengGeng
Copy link

我也遇到了类似的问题,现在 alova 的 params 总的数组会被转换成 1,2,3
我本以为 alova是在 adapter 中处理的,但实际上 alova 是在 /src/functions/sendRequest.ts[58] 的 /src/functions/sendRequest.ts 函数中进行了 url 的构建,是否可以像 axios 中 提供类似 paramsSerializer 的配置可以让用户进行配置

@JOU-amjs
Copy link
Contributor

我有一个新的想法,为什么不在beforeRequest中处理呢,这样会有更好的灵活度,例如你还可以用meta元数据来标识哪些需要序列化参数。

createAlova({
  // ...
  beforeRequest(method) {
    if (method.meta.serializeParams) {
      method.config.params = qs.stringify(method.config.params);
    }
  }
});

@LengGeng
Copy link

LengGeng commented Aug 21, 2024

我现在也是这样处理的,如果在代码中添加,最好在文档中写出示例方法

/**
 * 参数序列化方法
 * @param method method 实例
 */
function paramsSerializer(method: Method) {
  if (method.config.params) {
    const queryParamsStr = qs.stringify(method.config.params, { arrayFormat: "brackets" });
    if (queryParamsStr === "") return;

    method.url = method.url.includes("?") ? `${method.url}&${queryParamsStr}` : `${method.url}?${queryParamsStr}`;

    // 不删除的话 alova 里面还会对 `params` 再次处理
    delete method.config.params;
  }
}

@JOU-amjs
Copy link
Contributor

JOU-amjs commented Aug 21, 2024

@LengGeng 这个很不错,我觉得还可以让params支持string,这样不用手动自己处理拼接,也不需要手动delete了😎。文档内我再加下这个示例,熟悉的朋友可以接手这个改进,具体如下:

const method = alova.Get('/xxx', {
  params: 'a=1&b=2'
});

也可以在全局拦截器中将params转换为字符串。

createAlova({
  beforeRequest(method) {
    method.config.params = qs.stringify(method.config.params, { arrayFormat: "brackets" });
  }
})

@JOU-amjs JOU-amjs added the good first issue Good for newcomers label Aug 22, 2024
@Ya-hui
Copy link

Ya-hui commented Oct 28, 2024

@LengGeng 这个很不错,我觉得还可以让params支持string,这样不用手动自己处理拼接,也不需要手动delete了😎。文档内我再加下这个示例,熟悉的朋友可以接手这个改进,具体如下:

const method = alova.Get('/xxx', {
  params: 'a=1&b=2'
});

也可以在全局拦截器中将params转换为字符串。

createAlova({
  beforeRequest(method) {
    method.config.params = qs.stringify(method.config.params, { arrayFormat: "brackets" });
  }
})

method.config.params = qs.stringify(method.config.params, { arrayFormat: "brackets" }); 这个测试了吗? 我这边用起来还是编码了

?0=&&1=d&2=a&3=t&4=a&5==&6={&7=%22&8=s&9=c&10=e&11=n&12=e&13=%22&14=:&15=%22&16=i&17=t&18=e&19=m&20=%22&21=,&22=%22&23=o&24=f&25=f&26=e&27=r&28=I&29=d&30=%22&31=:&32=%22&33=7&34=3&35=7&36=8&37=7&38=7&39=8&40=1&41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug:confirmed Something isn't working good first issue Good for newcomers Vue Issues or pull request for Vue
Projects
None yet
Development

No branches or pull requests

5 participants