How do I extract the data from a promise

0

I am developing a project with angular, the assumption is that in a class I have the following method, which returns a promise of a string:

registrarCliente(cliente: Cliente): Promise<String> {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this.http.post("/api/taller/cliente/", cliente, options).toPromise().then(this.extractData);
}
//extrae el contenido del response
private extractData(res: Response) {
    let body = res.json();
    return body || {};
}

Then in another class I want to extract the string from that promise, so I have the following code:

    this.mensaje = this.clienteService.registrarCliente(cliente);
    this.mensaje.then(response => {
        alert(response);
    }).catch(e => {
        console.log(e);
    });

However, he never shows me the alert with the String and falls into the catch. PD: I have very little experience in this topic, if you can be clear when explaining it would be very helpful: D

---- I add --- With the console.log of the error shows me:

Object {
 _body: "<!DOCTYPE html>\r\n<html lang=\"es\" xmlns=\"http://www.w3.org/1999/xhtml\">\r\n    <head>\r\n        <meta charset=\"utf-8\" />\r\n        <title>Internal Server Error</title>\r\n        <style>\r\n            body {\r\n    font-family: 'Segoe UI', Tahoma, Arial, Helvetica, sans-serif;\r\n    font-size: .813em;\r\n    color: #222;\r\n    background-color: #fff;\r\n}\r\n\r\nh1, h2, h3, h4, h5 {\r\n    /*font-family: 'Segoe UI',Tahoma,Arial,Helvetica,sans-serif;*/\r\n    font-weight: 100;\r\n}\r\n\r\nh1 {\r\n    color: #44525e;\r\n    margin: 15px 0 15px 0;\r\n}\r\n\r\nh2 {\r\n    margin: 10px 5px 0 0;\r\n}\r\n\r\nh3 {\r\n    color: #363636;\r\n    margin: 5px 5px 0 0;\r\n}\r\n\r\ncode {\r\n    font-family: Consolas, \"Courier New\", courier, monospace;\r\n}\r\n\r\nbody .titleerror {\r\n    padding: 3px 3px 6px 3px;\r\n    display: block;\r\n    font-size: 1.5em;\r\n    font-weight: 100;\r\n}\r\n\r\nbody .location {\r\n    margin: 3px 0 10px 30px;\r\n}\r\n\r\n#header {\r\n    font-size: 18px;\r\n    padding: 15px 0;\r\n    border-top: 1px #ddd solid;\r\n    border-bottom: 1px #ddd solid;\r\n    m…",
 status: 500,
 ok: false,
 statusText: "Internal Server Error",
 headers: {…},
 type: 2,
 url: "http://localhost:62319/api/taller/cliente/" 
}       
    
asked by julio segura 11.07.2018 в 19:41
source

1 answer

0

There's nothing wrong with your code. The problem is actually on your server, there is an internal error, check the server log.

    
answered by 12.07.2018 в 07:54