Angular代理配置疑问:GET请求URL参数无法传递如何解决?
Great question - let's break down why your proxy isn't passing the path suffix and query parameters, and fix it right away.
The Root Issue
Your current proxy configuration doesn't include a pathRewrite rule, which means the full matched path (/address/cities) is being replaced by your target URL (https://api-url.com/address/) instead of appending the /cities portion and query parameters to it. That's why your logs show the proxy pointing to the base target URL without the rest of the request details.
Corrected proxy.conf.json
Update your proxy configuration to include a path rewrite rule that preserves the suffix of your request path:
{ "/address/*": { "target": "https://api-url.com/address/", "secure": false, "changeOrigin": true, "logLevel": "debug", "pathRewrite": { "^/address": "" } } }
How This Works
- The
pathRewrite: { "^/address": "" }rule tells the proxy to strip off the leading/addressfrom your request path. - So when your app sends a request to
/address/cities?cityName=&zipCode=2000&languageCode=NL, the proxy transforms it to/cities?cityName=&zipCode=2000&languageCode=NLand appends this to your target URL, resulting in the correct full request:https://api-url.com/address/cities?cityName=&zipCode=2000&languageCode=NL.
No Changes Needed to Your Request Code
Your existing fetch logic is perfectly fine - keep it as is:
const url = `/address/cities?cityName=&zipCode=2000&languageCode=NL`; const result = await fetch(url, { method: 'GET' }); return await result.json();
Verify the Fix
After updating the proxy config, restart your ng serve command. You should see logs like this, confirming the full path and parameters are being passed:
[HPM] GET /address/cities?cityName=&zipCode=2000&languageCode=NL -> https://api-url.com/address/cities?cityName=&zipCode=2000&languageCode=NL
内容的提问来源于stack exchange,提问作者Alex

