[英]POST form to FormSubmit with HttpClient and Angular
FormSubmit非常適合輕松提交輕量級表單。 我在我的 Angular 應用程序中使用它並嘗試使用HttpClient
發布表單,但我似乎無法正確處理。 我猜我的 POST url 是錯誤的,但我不知道正確的方法是什么。
這是我的onSubmit
function:
onSubmit() { this.submitted = true; if (this.contactForm.invalid) { return; } else { this.formSubmit.sendForm(JSON.stringify(this.contactForm.value)); } }
這里是HttpClient
服務:
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Injectable({ providedIn: 'root' }) export class FormsubmitService { constructor( public httpClient: HttpClient ) { } sendForm(formData) { console.log('Form Data:', formData); this.httpClient.post('https://formsubmit.io/send/<TOKEN HERE>', formData).subscribe( (response) => console.log("Response:", response), (error) => console.log("Error:", error)); } }
錯誤響應:
再會? 你能試試嗎?
onSubmit() {
this.submitted = true;
if (this.contactForm.valid) {
const formData = new FormData();
const { value } = this.contactForm;
for (const key in value) {
formData.append(key, value[key]);
}
this.formSubmit.sendForm(formData);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.