ionic2 make a query to an api when the view loads

0

I want to make the query to an api xxxxxx / yyy, just load a view, the problem is that when the page loads I do not receive an answer.

This is my code.

example.ts

  ionViewWillEnter() {

    this.chats.push("Hola prueba del chat");
    this.asistenteProvider.chat("Hola")
    .subscribe(res=>{
        console.log("Respuesta enviando mensajex2",res);
        this.chats.push(res);
    },error=>{
        console.log("error",error);
    });

  }

My provider

Example.provider.ts

export class EjemploProvider {
    url:string;
  constructor(public http: Http) {
    this.url="/chat";
    console.log('Hello');
  }

  chat(mensaje:string){
    console.log("Mensaje a enviar",mensaje);
    let headers = new Headers({ 'Content-Type': 'application/json' });

    let options = new RequestOptions({ headers: headers});

    return  this.http.post(
            this.url,
            {
                data:mensaje
            },
            options
            ).map(response=>{
                return response.json();
            }
            )

}
}

When the view loads while viewing the console, messages appear, but not the status if the request has been received or rejected.

When I send the first message I get an error and I do not receive a response

When I send more messages after 2 ° the answer I still get errors but the answer is given

Then when I sent messages, I no longer get errors and I receive the answer correctly.

I am using a proxy to make the query to the api, because it has not activated cors. What is the reason?

    
asked by Kevin AB 06.02.2017 в 01:06
source

1 answer

1

The problem should be in the links (location) to which the request is made to the API , verify in your browser if they are available and if it is allowed to do POST to the same.

    
answered by 12.04.2017 / 00:27
source