I’m trying to connect my ionic application to simple web service .NET.
Web service:
[WebMethod]
public string ValidarUsuario(string usuario, string password)
{
return “OK”;
}
typescript call:
let body = new FormData();
body.append(‘usuario’, user.email);
body.append(‘password’, user.password);
return this.http.post(Config.apiUrl + 'ValidarUsuario', body)
.map(this.extractData)
.subscribe(
data => console.log("Got data", data),
error => {console.error("Got error", error); this.handleErrors(error);}
);
I can connect to the service but the response always isTrusted = true.
[13:27:23] console.log: “{\n “isTrusted”: true\n}”
[13:27:23] console.log: [object Object]
[13:27:26] console.error: Got error
[object Object]
[13:27:28] console.error: [object Object]
Anybody could help me? Thanks.
That’s really strange, because you said the service returns “OK”.
“isTrusted = true” is error message.
I can’t read the response
I’m trying:
import { Injectable } from “@angular/core”;
import { Http, Headers, Response, RequestOptions } from “@angular/http”;
import { Observable } from “rxjs/Rx”;
import “rxjs/add/operator/do”;
import “rxjs/add/operator/map”;
import { User } from “./user”;
import ‘rxjs/Rx’;
import { Config } from “…/config”;
@Injectable()
export class UserService {
constructor(public http: Http) { }
headers = new Headers(
{
‘Content-Type’ : ‘application/json’
});
options = new RequestOptions({ headers: this.headers });
Prueba(user: User) {
let body = new FormData();
body.append(‘user’, user.email);
body.append(‘pass’, user.password);
let fullUrl = Config.apiUrl + “ValidarUsuario”;
console.log(“FUNCION PRUEBA”);
return new Promise((resolve, reject) => {
this.http.post(fullUrl, body, this.options)
.toPromise()
.then((response) =>
{
console.log('API Response : ', response);
resolve(response.text());
})
.catch((error) =>
{
console.error('API Error : ', error.toString());
reject(error.text());
});
});
}
}
If I include options the call fails and displays the following message:
[14:01:33] console.error: API Error : Response with status: 0 for URL: null
Error: Uncaught (in promise): {
“isTrusted”: true
}
at c (http://localhost:8100/build/polyfills.js:3:13535)
at http://localhost:8100/build/polyfills.js:3:12891
at http://localhost:8100/build/main.js:629:17
at t.invoke (http://localhost:8100/build/polyfills.js:3:9283)
at Object.onInvoke (http://localhost:8100/build/vendor.js:4656:37)
at t.invoke (http://localhost:8100/build/polyfills.js:3:9223)
at r.run (http://localhost:8100/build/polyfills.js:3:4452)
at http://localhost:8100/build/polyfills.js:3:14076
at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9967)
at Object.onInvokeTask (http://localhost:8100/build/vendor.js:4647:37)
but if i comment options parameter i can connect to service but the response is the same.
Have you checked if
let fullUrl = Config.apiUrl + “ValidarUsuario”;
build the url correct?
You’re lying about the content type. You’re saying it’s JSON, but then passing FormData
.
Thank you very much, I got it!
Login(user: User) {
let data = JSON.stringify({
user : user.email,
pass : user.password
});
let fullUrl = Config.apiUrl + "ValidarUsuario";
console.log("FUNCION PRUEBA");
console.log(fullUrl);
return new Promise((resolve, reject) => {
this.http.post(fullUrl, data, this.options)
.toPromise()
.then((response) =>
{
//console.log(response.text());
resolve(response.text());
})
.catch((error) =>
{
console.error('API Error : ', error.toString());
reject(error.text());
});
});
}
There is no need for explicitly instantiating a Promise
or manually stringifying the post body in the previous post.