如何在Angular 7中使用POST API将表单数据发送到服务器。

huangapple go评论76阅读模式
英文:

how to send a form data to server using post api in angular 7

问题

如何使用Angular发送表单数据使用POST API,已经在组件.ts文件中使用了表单和方法,

<form #hlForm="ngForm" (ngSubmit)="getHospitalList(hlForm)">
  <input 
   type="text" 
   class="input-texts" 
   name="name" 
   placeholder="输入医院、保险或地点"
   ngModel
   required
  >
  <button 
  type="submit" 
  value="submit" 
  class="hl-btn"
  (click)="hospitalFirstFold = false; hospitalSecondFold = true;"
   >搜索
   </button>
</form>

getHospitalList(form){
  console.log(form.value)
}
英文:

how can I send form data using post API using angular , have used form and method for component.ts file ,

&lt;form #hlForm=&quot;ngForm&quot; (ngSubmit)=&quot;getHospitalList(hlForm)&quot;&gt;
  &lt;input 
   type=&quot;text&quot; 
   class=&quot;input-texts&quot; 
   name=&quot;name&quot; 
   placeholder=&quot;Enter Hospital , Insurance or Location&quot;
   ngModel
   required
  &gt;
  &lt;button 
  type=&quot;submit&quot; 
  value=&quot;submit&quot; 
  class=&quot;hl-btn&quot;
  (click)=&quot;hospitalFirstFold = false; hospitalSecondFold = true;&quot;
   &gt;Search
   &lt;/button&gt;
&lt;/form&gt;

  getHospitalList(form){
    console.log(form.value)
  }

答案1

得分: 5

你需要创建表单数据以使用POST API发送数据。

let fd = new FormData();
fd.append(key, value);

对于API调用:

this.http.post(url, fd);

根据需要填写FormData中的值。

英文:

you need to create form data to send data using post api.

let fd = new FormData();
fd.append(key, value);

For api call

this.http.post(url, fd);

> Fill values accordingly in formdata

答案2

得分: 3

  1. 创建一个服务文件。
  2. 在你的类中创建一个自己选择的函数,例如,名为 function1 的函数。
function1(formData) {
return this.http.post(url, formData);
}

在构造函数中添加 http 并导入它。

constructor (private http: HttpClient) {}
  1. 在你的类中以表单作为参数调用函数 function1,并订阅它。
getHospitalList() {
this.serviceA.function1(form.value).subscribe(response) => 
  {console.log(response)});
}
英文:
  1. Create a service file.
  2. Create one function named of your choice, for example, function1 in your class.
function1(formData) {
return this.http.post(url, formData);
}

Add http in the constructor and import it.

constructor (private http: HttpClient) {}
  1. Call that function1 in your class with form as a parameter and subscribe
   getHospitalList() {
    this.serviceA.function1(form.value).subscribe(response) =&gt; 
      {console.log(response)});
    }


</details>



# 答案3
**得分**: 2

开始通过导入必要的API:

```typescript
import { HttpClient } from '@angular/common/http';

声明如下的提交函数:

getHospitalList(form){
    const formData = new FormData();
    formData.append('key', form.value);

    this.httpClient.post<any>(this.SERVER_URL, formData).subscribe(
      (res) => console.log(res),
      (err) => console.log(err)
    );
}
英文:

Start by importing the necessary APIs:

import { HttpClient } from &#39;@angular/common/http&#39;;

Declare submit function like below:

getHospitalList(form){
    const formData = new FormData();
    formData.append(&#39;key&#39;, form.value);

    this.httpClient.post&lt;any&gt;(this.SERVER_URL, formData).subscribe(
      (res) =&gt; console.log(res),
      (err) =&gt; console.log(err)
    );
  }

答案4

得分: 1

import HttpClientModule and FormsModule in your app module if you have not defined a different module.

import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';

in your component
import { NgForm } from '@angular/forms';

add serverurl as :
private readonly baseUrl = 'YourUrl';

constructor(private http: HttpClient) { }

create a method you gonna use for your submit event from your form:

getHospitalList(form: NgForm) {
this.http.post(this.baseUrl, form.value).subscribe(next => {
console.log("successful");
},
error => {
console.log("failed submitting");
});
}

(click)="hospitalFirstFold = false; hospitalSecondFold = true;" what is going on there?

英文:
import HttpClientModule and formsmodule in your app module if you have not defined a different module.

import { HttpClientModule } from &#39;@angular/common/http&#39;;
import { FormsModule } from &#39;@angular/forms&#39;;

*in your component*
import {NgForm} from &#39;@angular/forms&#39;;

add serverurl as :
private readonly baseUrl = &#39;YourUrl&#39;;

constructor(private http: HttpClient) { }

create a method you gonna use for your submit event from your for &quot;&quot;

  getHospitalList(form: NgForm) {
    this.http.post(this.baseUrl, form.value).subscribe(next =&gt; {
      console.log(&quot;successful&quot;);
    },
    error =&gt; {
      console.log(&quot;failed submitting&quot;);
    });
  }

(click)=&quot;hospitalFirstFold = false; hospitalSecondFold = true;&quot; what is going on there??

huangapple
  • 本文由 发表于 2020年1月6日 18:57:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定