我想使用PUT方法在angular文件中的JSON文件(本地JSON服务器)中添加数组:
这就是我所拥有的:
newsletter.service:
constructor(private http: HttpClient, private processHttpService: ProcessHTTPService) {
}
setNewsletterEmail(email): Observable<string> {
console.log('setNewsletterEmail()', JSON.stringify(email));
return this.http.put<string>(dataURL + 'newsletter', email, httpOptions)
.pipe(catchError(this.processHttpService.manageError));
}
getNewsletter(): Observable<string[]> {
return this.http.get<string[]>(dataURL + 'newsletter')
.pipe(catchError(this.processHttpService.manageError));
}
这是我的组件:
onSubmit() {
this.email = this.newsLetterForm.value.email;
console.log(this.newsLetterForm.value.email);
this.newsLetterForm.reset();
this.emailRest.push(this.email);
this.newsletterService.setNewsletterEmail(this.emailRest).subscribe(myEmails => {
this.email = myEmails;
console.log("My emails", myEmails);
});
}
emailRest
是在ngOnInit()
方法中设置的
return this.newsletterService.getNewsletter().subscribe(myNews => {
this.emailRest = myNews;
console.log("My Newsletter", myNews);
});
这是我的表格:
<form [formGroup]="newsLetterForm" (ngSubmit)="onSubmit()">
<input class="input" type="text" formControlName="email" name="newsletter" placeholder="Enter your email">
<button class="newsletter-btn"><i class="fa fa-paper-plane"></i></button>
</form>
控制台日志打印我要保存的阵列:
["correo@correo.com","correo2@correo2.com"]
这是错误:
PUT http://localhost:3000/newsletter 404 (Not Found)