typescript - Obteniendo error HttpErrorResponse en angular
Estoy intentando enviar una solicitud HTTP POST desde mi página createorder.component.ts para crear un registro. Pero estoy recibiendo un error HttpErrorResponse. ¿Dónde estoy equivocándome? Por favor, ayuda.
createorder.component.ts
onSubmit(form: any) {
this.spinner.show();
this.http.post('/api/order/' + '_foodforest' + '/' + '9ed7f9d5-8ed3-4f57-b360-7e4da87f2d6d' ,
JSON.stringify(form.value))
.subscribe(result => {
this.spinner.hide();
});
}
Error
Respuesta #1
Angular HttpClient intenta analizar la respuesta de la API en JSON por defecto.
La respuesta de tu API no está en formato JSON y JSON.parse falla dentro de HttpClient.
Puedes devolver la respuesta de la API como JSON desde el backend o debes decirle al cliente HTTP que el tipo de respuesta no es JSON:
this.http.post(url, JSON.stringify(form.value), {
responseType: 'text'
})
Respuesta #2
onSubmit(form: any) {
const url=''; // replace with your endpoint
const headers = new Headers();
// headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Content-Type', 'text/plain; charset=utf-8');
this.spinner.show();
this.http.post(url,JSON.stringify(form.value), { headers: headers })
.subscribe(result => {this.spinner.hide();});
}
Prueba el fragmento de código anterior con el tipo de contenido que necesitas text/plain o application/x-www-form-urlencoded
