I want to query an api xxxxxx/yyy, as soon as a view loads, the problem is that when the page loads I don't receive a response.
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, seeing the console, the 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 don't receive a response
When I send more messages from the 2nd, the answer still gives me errors but the answer is given
Then when I sent messages I no longer get errors and I receive the response correctly.
I am using a proxy to be able to make the query to the api , since this does not have cors activated. For what is this?.
The problem must be in the links (location) to which the request is made to the API , check in your browser if they are available and if it is allowed to POST to it.