我在编辑模式下提交表单时遇到两个错误。
JSON的意外结束
输入JSON输入的意外结束
更新工作正常,并在数据库中保存值只是我可以在控制台中看到此消息并在提交不起作用后路由重定向。这是代码服务和组件文件。
threat.service.ts
updateThreat(threat: Threat): Observable<Threat> {
var requestOptions = this.authService.requestOptionsWithToken();
return this.http.put(this.serviceUrl + 'api/threat', threat, requestOptions)
.map(extractData)
.catch(handleError);
}
public extractData(res: any) {
return res.json() || [];
}
public handleError(error: Response | any) {
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
添加 - 编辑威胁component.ts
import { Component, Inject, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { Threat } from '../../../../models/threat.model';
import { ThreatService } from '../../../../services/threat.service';
import { ThreatComponent } from '../threat/threat.component';
@Component({
selector: 'app-add-edit-threat',
templateUrl: './add-edit-threat.component.html',
styleUrls: ['./add-edit-threat.component.css']
})
export class AddEditThreatComponent implements OnInit {
threatCategories: any[];
threatForm: FormGroup;
id: number;
editMode: boolean = false;
typeOfThreat = [
{ value: 1, name: 'Security' },
{ value: 2, name: 'Safety' }
];
originOfThreat = [
{ value: 1, name: 'Internal' },
{ value: 2, name: 'External' },
{ value: 3, name: 'Both' }
];
constructor(private fb: FormBuilder,
private threatService: ThreatService,
private router: Router,
private route: ActivatedRoute) {
if (this.route.snapshot.params["id"]) {
this.id = +this.route.snapshot.params["id"];
}
this.threatForm = this.fb.group({
threatId: 0,
name: ['', [Validators.required]],
category: ['', [Validators.required]],
description: [''],
internalExternal: [1, Validators.required],
securitySafety: [3, Validators.required],
avoidRisk: [false],
shareRisk: [false],
reduceRisk: [false],
acceptRisk: [false],
confidenciality: [false],
integrity: [false],
availability: [false],
authenticity: [false],
})
}
ngOnInit() {
this.onGetThreatsCategory();
if (this.id > 0) {
this.editMode = true;
this.threatService.getThreat(this.id)
.subscribe(data => this.threatForm.patchValue(data))
}
}
onGetThreatsCategory() {
this.threatService.getThreatCategory()
.subscribe(
response => {
this.threatCategories = response.items;
});
}
onSubmit() {
if (!this.threatForm.valid) {
return
}
if (!this.editMode) {
this.threatService.createThreat(this.threatForm.value)
.subscribe((data) => {
this.router.navigate(['/threat']);
console.log(this.threatForm);
})
}
else if (this.editMode) {
this.threatService.updateThreat(this.threatForm.value)
.subscribe((data) => {
this.router.navigate(['/threat']);
console.log(this.threatForm);
})
}
}
}
答案 0 :(得分:0)
更新:
当我改变功能时
public extractData(res: any) {
return res.json() || [];
}
与
.map((response: Response) => response)
updateThreat函数中的错误已消失,一切正常。
updateThreat(threat: Threat): Observable<Threat> {
var requestOptions = this.authService.requestOptionsWithToken();
return this.http.put(this.serviceUrl + 'api/threat', threat, requestOptions)
.map((response: Response) => response)
.catch(this.dataService.handleError);
}