POST 使用 HttpClient 和 Angular 将表单提交到 FormSubmit

POST form to FormSubmit with HttpClient and Angular

FormSubmit 非常适合轻松提交轻量级表单。我在我的 Angular 应用程序中使用它并尝试 POST 带有 HttpClient 的表单,但我似乎无法正确处理。我猜我的 POST url 是错误的,但我不知道正确的方法是什么。

这是我的 onSubmit 函数:

  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);
  }
}