HTTP 400 错误请求 分析值 Date 时遇到意外字符



我在将日期从我的角度应用程序发布到我的 ASP.net Web API 时遇到问题。我将数据记录到控制台,出现以下错误:

接口响应错误

不完全确定我尝试发布的日期数据有什么问题。格式错误吗?我正在使用 Angular 日期时间选择器。

我认为导致问题的"日期发现"字段的模型:

export class Nclog {
id: number;
DateFound?:  Date;
LocationFound: string;
NCTypeId: number;
NCDescription: string;
ActionTaken: string;
CauseOfNc: string;
InvestigatedBy: string;
InvestigationStart?: Date;
InvestigationFinish?: Date;
CorrectiveAction: string;
AddressRootCause: string;
PreventsReoccurrance: string;
Valid: boolean;
ImplementedEffective: boolean;
FollowUpComments: string;
Effective: boolean;
EffectivenessComments: string;
Signed: string;
Date?: Date;
NCStatus: number;
NCNumber: number;
NotedBy: string;
DateTest: Date;
}

服务网

import { Injectable } from '@angular/core';
import { Nclog } from './nclog.model';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class NclogService {
nclogFormData: Nclog;
list: Nclog[];
readonly rootURL = "https://localhost:44322/api"
constructor(private http: HttpClient) { }
postLog(nclogFormData: Nclog) {
console.log(JSON.stringify(nclogFormData));
nclogFormData.id = 0;
return this.http.post(this.rootURL+'/NCLog', nclogFormData);
}
refreshList(){
this.http.get(this.rootURL+'/NCLog')
.toPromise().then(res => this.list = res as Nclog[]);
}

.HTML

<div class="form-group col-md-6">
<label>Date NC Found</label>
<input class="form-control" placeholder="yyyy-mm-dd"
name="DateTest" #DateFound="ngModel" [(ngModel)]="service.nclogFormData.DateFound" ngbDatepicker #d="ngbDatepicker">
<div class="input-group-append">
<button class="btn btn-outline-secondary calendar" (click)="d.toggle()" type="button"></button>
</div>

好的,所以答案是 API 的日期选择器格式是错误的。

所以我在发布日期字段之前使用 moment.js 来格式化日期字段。

postLog(nclogFormData: Nclog) {
console.log(JSON.stringify(nclogFormData));
nclogFormData.id = 0;
nclogFormData.DateFound = moment().format();
return this.http.post(this.rootURL+'/NCLog', nclogFormData);
}

最新更新