如何将表单数据发布到外部rest API?
目前我有一个HTML表单:
<ion-content padding>
<ion-grid fixed>
<ion-row justify-content-center align-items-center nowrap>
<ion-col col-auto>
<h2 text-capitalize>quick order</h2>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<form #formQuickOrder="ngForm">
<ion-list>
<ion-item *ngFor="let inputElem of templateData">
<!-- <ng-container *ngFor="let inputElem of templateData"> -->
<ion-label color="primary" floating>
<ion-icon name="{{inputElem.icon}}"></ion-icon>
<ion-note text-capitalize>{{inputElem.label}}</ion-note>
</ion-label>
<ion-input *ngIf="inputElem.name == 'name' else F" #input (ionBlur)="onInputBlur(input)" [type]="inputElem.type" name="inputElem.name" [(ngModel)]="inputElem.model" ngModel required minLength="3" maxLength="50"></ion-input>
<ng-template #F><ion-input #input (ionBlur)="onInputBlur(input)" [type]="inputElem.type" name="inputElem.name" [(ngModel)]="inputElem.model" ngModel required></ion-input></ng-template>
<!-- <span *ngIf="!input.ngControl.valid && isSentOrderClicked">{{input.getNativeElement().children[0].validationMessage}}</span> -->
<!-- </ng-container> -->
</ion-item>
</ion-list>
</form>
</ion-col>
</ion-row>
</ion-grid>
<div class="btn-holder">
<button (click)="onOrder()" class="btn-order-now" ion-button color="dark" outline round>order now</button>
</div>
</ion-content>
然后我有我在component.ts文件中处理提交的函数:
onOrder(formQuickOrder: NgForm) {
console.log(formQuickOrder)
//this.http.post('http://xxx/externalapi/add', formQuickOrder);
}
但是如何将表单数据发布到我的外部API呢?使用angular发送表单数据的标准是什么?它只是一个简单的post请求,表单数据为queryParams,或者将其转换为JSON是标准的。我可以修改API来处理发送的任何数据,这不是问题。
答案 0 :(得分:0)
您可以使用有角度的HttpClient并使用 POST 方法将数据发送给支持。
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) { }
pubic onOrder(formQuickOrder: NgForm): void {
this.postData(pass your form value here);
}
priavte postData (formData: any): Observable<any> {
return this.http.post<any>(this.yourUrl, formData);
}