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

Bug: RequestClient GET请求数组参数 报错 #5702

Open
5 tasks done
elkon028 opened this issue Mar 11, 2025 · 2 comments
Open
5 tasks done

Bug: RequestClient GET请求数组参数 报错 #5702

elkon028 opened this issue Mar 11, 2025 · 2 comments

Comments

@elkon028
Copy link

Version

Vben Admin V5

Describe the bug?

Access to XMLHttpRequest at 'http://localhost:48090/admin-api/system/dict-type/page?pageNo=1&pageSize=20&createTime[]=2025-03-11+00:00:00&createTime[]=2025-03-13+00:00:00' from origin 'http://localhost:5666' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
RequestClient能不能集成 qs 来解决这个问题

pnpm add qs
pnpm add -D @types/qs


import qs from 'qs'

export const requestClient = createRequestClient(apiURL, {
  responseReturn: 'data',
  // axios get请求 params 带数组参数的处理
  // from origin 'url' has been blocked by CORS policy
  paramsSerializer(params) {
    // 'repeat' | 'comma' | 'brackets' | 'indices'
    return qs.stringify(params, { arrayFormat: 'repeat' })
  },
})

// 最好是集成到 RequestClient 类的参数里
export const requestClient = createRequestClient(apiURL, {
  responseReturn: 'data',
 // 给 RequestClient 一个参数,免得用户还得安装 qs 包
  paramsSerializer: 'comma',  // 'repeat' | 'comma' | 'brackets' | 'indices'
})

Reproduction

null

System Info

all

Relevant log output

Validations

@mynetfan
Copy link
Collaborator

你这个报错是跨域导致的

@elkon028
Copy link
Author

elkon028 commented Mar 12, 2025

你这个报错是跨域导致的

应该不是跨域问题,如果是跨域问题,那么无论传什么参数都会报跨域的错误

然而只有含有数组参数
如: page?createTime[]=2025-03-01&createTime[]=2025-03-02 会报错

page?createTime[1]=2025-03-01&createTime[2]=2025-03-02 不会报错
page?createTime=2025-03-01&createTime=2025-03-02 不会报错

https://www.cnblogs.com/nayek/p/12284036.html 解决方案参考这个

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants