POST表单到带有HttpClient和Angular的FormSubmit

问题描述 投票:-1回答:1

FormSubmit非常适合简单的轻量级表单提交。我在Angular应用程序中使用它,并尝试使用HttpClient发布表单,但似乎无法正确完成。我猜我的POST网址是错误的,但我无所适从,弄清楚什么是正确的方法。

这是我的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));
  }
}

错误响应:

enter image description here

enter image description here

javascript angular post form-submit angular-httpclient
1个回答
0
投票

美好的一天!你可以尝试一下吗?

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);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.