You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular代理配置疑问:GET请求URL参数无法传递如何解决?

Fixing Angular Proxy Path & Parameter Passing

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 /address from 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=NL and 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:33:22