日安,
我已经做了一些自己的研究角7和Asp.net核心网络api。
现在我想阅读asp.net核的结果,并显示在角7中。
这是来自Asp.net核心的代码
[HttpPost("DataCorrection")]
public IActionResult DataCorrection([FromBody] DataCorrectionDto data)
{
try
{
var request = Request;
var values = _dataCorrection.GetDateRange(data.StartDate, data.EndDate);
return Ok(values);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
throw ex;
}
}方法GetDateRange返回一个DataCorrection Model列表
这是我的角码
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-data-correction',
templateUrl: './data-correction.component.html',
styleUrls: ['./data-correction.component.css']
})
export class DataCorrectionComponent implements OnInit {
list: any[];
selectedDate = new Date();
form = new FormGroup({
StartDate: new FormControl(),
EndDate: new FormControl()
});
constructor(private http: HttpClient) { }
ngOnInit() {
}
onSubmit() {
this.http.post('http://localhost:5000/api/DataCorrection/DataCorrection', this.form.value)
.toPromise()
.then(res => {
this.list = res as any[];
console.log('Called');
});
}
}这是Html代码
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<!-- <input formControlName="first" [(ngModel)]="value"> -->
<mat-form-field>
<input matInput formControlName="StartDate" [matDatepicker]="StartDate" placeholder="Start date">
<mat-datepicker-toggle matSuffix [for]="StartDate"></mat-datepicker-toggle>
<mat-datepicker #StartDate ></mat-datepicker>
</mat-form-field>
<mat-form-field>
<input matInput formControlName="EndDate" [matDatepicker]="EndDate" placeholder="End date">
<mat-datepicker-toggle matSuffix [for]="EndDate"></mat-datepicker-toggle>
<mat-datepicker #EndDate ></mat-datepicker>
</mat-form-field>
<div class="form-group">
<button class="btn btn-primary">Submit</button>
</div>
</form>
<tr *ngFor="let pd of list">
<td >{{pd.ActualLogin}}</td>
<td >{{pd.ActualLogout}}</td>
<td >{{pd.ShiftLogin}}</td>
<td>
<i class="far fa-trash-alt fa-lg text-danger" ></i>
</td>
</tr>现在,我如何正确地阅读它,这样我就可以将它加载到一个表中。
上面的密码。它甚至没有填充表。但是它叫console.log
我已经找了好几个小时了。但我迷路了。我不明白这些教程,我也不能让它起作用。
这是我的web的响应。我从chrome web浏览器中的response选项卡中读取它。

{"emp_Id":963,"actualLogin":"05:00:11","actualLogout":"05:01:00","shiftLogin":"05:00:00","shiftLogout":"13:00:00","date":"2019-04-03T00:00:00Z"}这些数据是我希望在表中填充的数据。
谢谢。
发布于 2019-04-03 05:58:17
我不认为这是问题所在
模板pd.ActualLogin中有拼写错误,但实际上在JSON响应中拼写为actualLogin.将模板更改为
<tr *ngFor="let pd of list">
<td >{{pd.actualLogin}}</td>
<td >{{pd.actualLogout}}</td>
<td >{{pd.shiftLogin}}</td>
<td>
<i class="far fa-trash-alt fa-lg text-danger" ></i>
</td>
</tr>发布于 2019-04-03 04:39:31
将结果保存在某个变量中
示例
this.http.post('http://localhost:5000/api/DataCorrection/DataCorrection', this.form.value)
.toPromise()
.then(res => {
result = res;
console.log('Res ' + res);
});并尝试在UI中显示
示例
<div>{{result|json}}</div>这将以json格式显示结果。
注意:使用ngFor根据输出格式在表中显示结果
发布于 2019-04-03 04:54:28
您正在进行跨域请求,因为您的api和角应用程序被服务在不同的端口上。您可以添加一个cors头以接受api上的所有域(不推荐),也可以设置一个proxy.config.json文件,以便将您的api调用重定向到与您的角度应用程序相同的端口。
{
"/api/*": {
"target": "http://localhost:5000/api/",
"pathRewrite": {
"^/api": ""
},
"changeOrigin": true,
"secure": false,
"logLevel": "debug"
}
}在您的角度使用/api/数据校正/数据校正,而不是http://localhost:5000/api/DataCorrection/DataCorrection
然后与.
ng serve -代理-config proxy.config.json
https://stackoverflow.com/questions/55487118
复制相似问题