Angular 的不安全 HTTP 请求
Insecure HTTP Request With Angular
我正在尝试通过 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"
]
}
}
N.B:上面的 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!");
//});
}
}
更新 2:jSon
数据 - 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,例如数组。
您可以在您的服务器端代码中启用跨域,这对您的尝试很有用
尝试在显示循环中使用 Elvis 运算符:
<tr *ngFor="let value of default">
<td>{{ value?.userId }}</td>
<td>{{ value?.title }}</td>
</tr>
item
之后的 ?.
(在本例中为 value
)确保 Angular 不会尝试呈现数据 直到 数据显示,因此 value?.userId
将在 value.userId
失败的地方工作,因为在呈现视图时未定义值,并且 Angular 不知道 [=17] =] 属性直到获取完成,此时它已经抛出错误。
此外,当使用 http 和 result.json() 时,您将获得一个数据对象数组,因此您不需要 as object[];我也倾向于将值键入任何[],看看是否行不通。
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));
}
我正在尝试通过 ASP.NET Core
学习 Angular
并且有一个基本的项目启动。现在我 运行 进入一个我猜想的非常基本的问题,需要很少的修改。我创建了一个 Web api 项目并将其托管在 IIS - 内部服务器中。所以我在 Angular
:
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"
]
}
}
N.B:上面的 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!");
//});
}
}
更新 2:jSon
数据 - 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,例如数组。
您可以在您的服务器端代码中启用跨域,这对您的尝试很有用
尝试在显示循环中使用 Elvis 运算符:
<tr *ngFor="let value of default">
<td>{{ value?.userId }}</td>
<td>{{ value?.title }}</td>
</tr>
item
之后的 ?.
(在本例中为 value
)确保 Angular 不会尝试呈现数据 直到 数据显示,因此 value?.userId
将在 value.userId
失败的地方工作,因为在呈现视图时未定义值,并且 Angular 不知道 [=17] =] 属性直到获取完成,此时它已经抛出错误。
此外,当使用 http 和 result.json() 时,您将获得一个数据对象数组,因此您不需要 as object[];我也倾向于将值键入任何[],看看是否行不通。
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));
}