使用 HttpClient 和 Angular 将表单发布到 FormSubmit

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

  }

}

错误响应:

http://img3.mukewang.com/62aad824000135f213140147.jpg

http://img.mukewang.com/62aad82c000104f913110262.jpg


白衣染霜花
浏览 217回答 1
1回答

慕丝7291255

再会!你能试试吗?onSubmit() {&nbsp; this.submitted = true;&nbsp; if (this.contactForm.valid) {&nbsp; &nbsp; const formData = new FormData();&nbsp; &nbsp; const { value } = this.contactForm;&nbsp; &nbsp; for (const key in value) {&nbsp; &nbsp; &nbsp; formData.append(key, value[key]);&nbsp; &nbsp; }&nbsp; &nbsp; this.formSubmit.sendForm(formData);&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript