http 400错误请求分析值日期时遇到意外字符

3zwtqj6y  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(317)

从angular应用程序向asp.net web api发布日期时遇到问题。我已将数据记录到控制台,出现以下错误:
api响应错误
我不完全确定我要发布的日期数据有什么问题。是不是格式不对?我用的是有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;
}

服务.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>
zour9fqk

zour9fqk1#

好的,所以这个问题的答案是datepicker格式对于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);

  }

相关问题