I have form, and I want post form values to API, but the values can not be posted, how can resolve this error?
- my html form:
<form #adduser ="ngForm" (ngSubmit)="onAddUser(adduser.value)">
<mat-form-field class="form-fild-width">
<mat-label>First Name</mat-label>
<input matInput name="first_name" ngModel required>
</mat-form-field>
<mat-form-field class="form-fild-width">
<mat-label>Last Name</mat-label>
<input matInput name="last_name" ngModel required>
</mat-form-field>
<mat-form-field class="form-fild-width">
<mat-label>User Name</mat-label>
<input matInput name="user_name" ngModel required>
</mat-form-field>
<mat-form-field class="form-fild-width">
<mat-label>Password</mat-label>
<input matInput name="password" ngModel required>
</mat-form-field>
<mat-action-row>
<button mat-raised-button color="primary">Add User</button>
</mat-action-row>
</form>
- my typescript code:
onAddUser(data:{first_name: string, last_name: string, user_name: string, password: string}){
console.log(data)
this.http.post('https://my.users.com/users/add?', data,{headers:headers})
.subscribe((result) => {
console.warn(result);
})
}
As mentioned that you pass the form value as the query string param, your current implementation in Angular is incorrect as you post the form value in the request body.
You should build the URL with the query string param.
Another approach is applying the
HttpParamsto pass the form value as the query string.