1

Ich frage mich, wie bekomme ich den Körper einer HTTP-POST-Anfrage in Angulars2. Wenn ich zurück zur Fehler-Methode komme, ist der Körper zugänglich.Angular2 http Post erhalten Sie den Körper der Anfrage

return this.http.post(requestUrl, **body**, options) 
.map((res:Response) => res.json(), 
    (err)=> { 
     return {"errorObj":err, "requestBody":**body**} 
    }) 

Danke.

Antwort

0

Sie können dies testen. Arbeiten. gibt dir die Körperanfrage und die URL.

this.http.post(url, body, this.options).subscribe(
    data => { 
    console.log('ok url:' + url); 
    console.log('body:' + JSON.stringify(body)); 
    }, 
    (err: HttpErrorResponse) => { 
    if (err.error instanceof Error) { 
     // A client-side or network error occurred. Handle it accordingly. 
     console.log('instanceof error url:' + url); 
     console.log('body:' + JSON.stringify(body)); 
     console.log('An error occurred:', err.error.message); 
    } else { 
     // The backend returned an unsuccessful response code. 
     // The response body may contain clues as to what went wrong, 
     console.log('backend error url:' + url:' + url); 
     console.log('body:' + JSON.stringify(body)); 
     let strBody = JSON.stringify(err); 
     strBody = JSON.stringify(err.error); 
     strBody = JSON.stringify(err.status); 
     console.log(`Backend returned code ${err.status}, body was: ${err.error}`); 
    } 
    } 
);