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

求助:Angular HttpClient无法读取响应中的Location头

Why the Location Header Isn't Accessible in Angular's HttpClient

Ah, I've run into this exact issue with Azure Functions and Angular before! The problem isn't with your Angular code—it's a CORS restriction that's hiding the Location header from your frontend, even though Fiddler can see it in the raw response.

Here's the breakdown:

Browsers enforce strict CORS rules for security. By default, only a small set of "safe" response headers (like Content-Type, Cache-Control, etc.) are exposed to client-side JavaScript. Headers like Location aren't in that default list, so even if your backend sends them, Angular's HttpClient won't have access to them unless you explicitly tell the browser it's okay.

Fixing it with Azure Functions

Even though Azure Functions allows all headers via Access-Control-Allow-Headers, that setting controls request headers the backend accepts. You need to configure Access-Control-Expose-Headers to list the Location header so the browser exposes it to your frontend.

You have two ways to set this up:

  1. Local emulator (local.settings.json)
    Open your local.settings.json file and add the Access-Control-Expose-Headers to your Values section:

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "",
        "FUNCTIONS_WORKER_RUNTIME": "node", // or your runtime of choice
        "Access-Control-Expose-Headers": "Location"
      }
    }
    

    Restart your emulator after making this change.

  2. Add it directly in your function's response
    If you want more granular control, you can manually add the header in your Azure Function code. For example, in Node.js:

    context.res = {
      status: 201,
      headers: {
        "Location": "/your-resource-path",
        "Access-Control-Expose-Headers": "Location"
      }
    };
    

    Or in C#:

    return new HttpResponseMessage(HttpStatusCode.Created)
    {
        Headers = {
            Location = new Uri("/your-resource-path", UriKind.Relative),
            { "Access-Control-Expose-Headers", "Location" }
        }
    };
    

Verify the fix

After configuring this, send your POST request again. Your Angular code should now be able to retrieve the Location header using response.headers.get('Location') without issues.

内容的提问来源于stack exchange,提问作者Oddleif

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:45:44