HTTP 400 错误请求解析值日期时遇到意外字符
HTTP 400 bad request Unexpected character encountered while parsing value Date
我在 post 从我的 angular 申请到我的 ASP.net 网站 api 时遇到问题。我将数据记录到控制台,但出现以下错误:
API Response error
不完全确定我尝试的日期数据有什么问题post。它的格式有误吗?我正在使用 Angular 日期时间选择器。
我认为导致问题的 'DateFound' 字段的模型:
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;
}
Service.ts
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);
}
我在 post 从我的 angular 申请到我的 ASP.net 网站 api 时遇到问题。我将数据记录到控制台,但出现以下错误:
API Response error
不完全确定我尝试的日期数据有什么问题post。它的格式有误吗?我正在使用 Angular 日期时间选择器。
我认为导致问题的 'DateFound' 字段的模型:
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;
}
Service.ts
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);
}