我有一个托管在 Azure 中的 .NET Core API。在本地运行 Angular UI(版本 8)时,我想将 API 请求指向在 Azure 中运行的应用程序服务。为此,我设置了 Angular proxy.conf.json 文件,如下所示:
{
"/api": {
"target": "http://my-app.azurewebsites.net",
"secure": false,
"logLevel": "debug",
"changeOrigin": "true"
},
当我在本地运行 UI 并尝试登录时,我看到 Chrome 的网络调试器有以下 3 个请求:
[General]
Request URL: http://localhost:4200/api/account/login
Request Method: GET
Status Code: 301 Moved Permanently
[Response Headers]
location: https://my-app.azurewebsites.net/api/account/login
server: Microsoft-IIS/10.0
---
[General]
Request URL: https://my-app.azurewebsites.net/api/account/login
Request Method: OPTIONS
Status Code: 204 No Content
[Response Headers]
Access-Control-Allow-Methods: GET
Access-Control-Allow-Origin: http://localhost:4200
---
[General]
Request URL: https://my-app.azurewebsites.net/api/account/login
Request Method: GET
Status Code: 404 Not Found
特别奇怪的是我的登录请求(从我的 UI 调用)是 POST,而不是 GET。
我在许多项目中配置了 Angular 的开发服务器代理,但以前从未见过这种情况。感觉好像我错过了一些明显的东西..
最佳答案
我忘记了我最近将我的 Azure 应用程序更新为“仅 HTTPS”!由于我的代理配置为将流量发送到“http”,因此 Azure 服务器应返回 301。
一旦我更改了代理配置文件以说明一切都按预期工作:
{
"/api": {
"target": "https://my-app.azurewebsites.net", <-- https now
"secure": false,
"logLevel": "debug",
"changeOrigin": "true"
},
了解了这一点,我在 Chrome 中看到的 3 个 HTTP 请求的详细信息就更有意义了 - 我可以看到 Azure 返回 301,将我重定向到相同的路由,但这次使用“https”。
关于angular - 运行 Angular 开发服务器时,为什么我的 API 调用会返回 301 状态代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56672185/