【问题标题】:Insecure HTTP Request With Angular使用 Angular 的不安全 HTTP 请求
【发布时间】:2019-01-15 16:46:42
【问题描述】:

我正在尝试用ASP.NET Core 学习Angular,并且有一个基本的项目启动。现在我遇到了一个非常基本的问题,我猜它需要很少的修改。我创建了一个 web api 项目并将其托管在 IIS - 内部服务器中。所以我在Angular中使用了如下api:

import { Component, Inject } from '@angular/core';
import { Http } from '@angular/http';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})

export class AppComponent {
    title = 'Our First Angular App';
    public values: object[];

    constructor(private http: Http) {
      this.http.get('http://192.168.10.100/api/values').subscribe(result => { //Calling the web api here as jSon
        this.values = result.json() as object[];
        }, error => console.error(error));
    }
}

上面没有用Angular在前端获取数据。相反,当使用浏览器的检查元素进行调试时,它会抛出异常:

http://192.168.10.100/api/values - http request was insecure

我可以理解,它可能需要集成 https 或某种安全性。但就目前而言,有什么办法可以让上面的代码与 http 请求一起工作?如果有帮助,我将包括以下配置文件:

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "OurFirstAngularApp": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "wwwroot",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css",
              "C:\\Users\\AT\\Documents\\Visual Studio 2017\\Projects\\OurFirstAngularApp\\OurFirstAngularApp\\node_modules\\bootstrap\\dist\\css\\bootstrap.min.css"
            ],
            "scripts": [
              "C:\\Users\\AT\\Documents\\Visual Studio 2017\\Projects\\OurFirstAngularApp\\OurFirstAngularApp\\node_modules\\bootstrap\\dist\\js\\bootstrap.min.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "OurFirstAngularApp:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "OurFirstAngularApp:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "OurFirstAngularApp:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.css"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "OurFirstAngularApp-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "prefix": "",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "OurFirstAngularApp:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "OurFirstAngularApp:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "OurFirstAngularApp"
}

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

注意:上面的 api 在请求时工作正常,我的意思是完美地获取 jSon 数据。

更新 1:Startup.cs

public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, 
    // visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();

        app.UseDefaultFiles();
        app.UseStaticFiles();
        //app.Run(async (context) =>
        //{
        //  await context.Response.WriteAsync("Hello World!");
        //});
    }
}

更新 2jSon 数据 - Raw jSon Data

现在我明白了。基本上它是在进行 http 调用,出于安全原因,它需要 https。所以我尝试了一个包含原始jSon 数据的 https 服务。但是当我完成以下操作时:

public default: object[];

constructor(private http: Http) {
    this.http.get('https://jsonplaceholder.typicode.com/todos/1/').subscribe(result => {
        this.default = result.json() as object[];
    }, error => console.error(error));
}

终于在前端加上Angular

<tr *ngFor="let value of default">
   <td>{{ value.userId }}</td>
   <td>{{ value.title }}</td>
</tr>

上面在使用浏览器的检查元素时抛出了异常,如下所示 - 错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 只支持绑定到数组等 Iterables。

【问题讨论】:

  • 无论是http 还是https,它都应该可以工作,我看到你标记了asp.net core,也许你在启动时有一些https 配置?
  • 我已经用 Startup.cs 文件 @penleychan 更新了帖子。看看这是否是我需要配置的东西。
  • startup.cs 看起来很简单,Angular 和你的 api 在 http 上运行的不是混合内容吗?
  • 我会从错误中猜测 IIS 服务器需要一个 https 请求,而您正在发出一个 http 请求,所以那里不匹配。您是否尝试过将 this.http.get('192.168.10.100/api/values 调用更改为 this.http.get('192.168.10.100/api/values?
  • @AT-2017 您是否尝试在订阅回调中的this.values = result.json() as object[]; 行设置断点以检查api 是否返回有效数据?

标签: asp.net angular asp.net-core angular7


【解决方案1】:

尝试在显示循环上使用 Elvis 运算符:

<tr *ngFor="let value of default">
   <td>{{ value?.userId }}</td>
   <td>{{ value?.title }}</td>
</tr>

item 之后的 ?.(在本例中为 value)确保 Angular 不会尝试渲染数据直到数据出现,因此 value?.userId 将在 value.userId 将失败的工作,因为在呈现视图时值未定义,并且 Angular 在获取完成之前不知道 .userID 属性,因此它已经抛出了错误。

此外,当使用 http 和 result.json() 时,您将获得一个数据对象数组,因此您不需要 as object[];我也倾向于将值键入为 any[],看看这是否行不通。

public values: any[];

constructor(private http: Http) {
  this.http.get('http://192.168.10.100/api/values').subscribe(result => { //Calling the web api here as json
    this.values = result.json();
    }, error => console.error(error));
}

【讨论】:

  • 试过@Stephen R. Smith 但得到同样的错误 - 错误:找不到类型为“object”的不同支持对象“[object Object]”。 NgFor 仅支持绑定到诸如数组之类的 Iterables。 现在特别针对帖子更新 2 中的原始 jSon 数据。
  • this.values = result.json();行,添加一个 console.log("Data: ",this.values);行并检查浏览器中的控制台以查看输出内容,然后将其添加到问题中以便我们查看。这需要是一个有效的对象数组 - 你得到的错误基本上是告诉你 NgFor 需要一个数组来迭代,它没有得到一个。
  • 非常感谢 @Stephen R. Smith,返回的数据如下所示 数据:对象已完成:错误 ID:1 标题:“delectus aut autem”用户 ID:1乙>。最后在Angular 中做到了这一点 - {{ default.userId }} {{ default.title }} 没有循环。
  • 太棒了!注销数据,尤其是当您从服务中取回数据时,是解决此类问题的好方法,因为通常您期望的属性名称,或者数据的结构与您期望的略有不同,并且如果它不完全匹配,它将无法工作。很高兴它正在工作!
【解决方案2】:

您可以在您的服务器端代码中启用跨域,这对您很有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-19
    • 2021-07-07
    • 1970-01-01
    • 2018-02-06
    • 2017-09-17
    • 2017-05-05
    • 1970-01-01
    • 2017-08-07
    相关资源
    最近更新 更多