Error reading Json in Ionic

Asked

Viewed 65 times

0

Good evening, I have an Ionic project that would communicate with a php, but I’m not able to read the data returned by php in Ionic.

function on home page.ts

realizarLogin() {
    this.authServiceProvider.logar(this.model.email, this.model.password)
      .then((result: any) => {

        console.log(result);

        if(result.data == "00-4A"){ 
          this.toast.create({ message: 'Usuário desativado', position: 'botton', duration: 3000 }).present();
        }
        else if(result.data == "00-3A")
        {
          this.toast.create({ message: 'Usuário e/ou senha inválidos.', position: 'botton', duration: 3000 }).present();
        }
        else 
        {
          this.toast.create({ message: 'registro '+ result.data, position: 'botton', duration: 3000 }).present();
          this.navCtrl.push('MenuInternoPage', { result: user.data });
        }
      }) 
      .catch((error: any) => {
        this.toast.create({ message: 'Erro ao efetuar login. Erro: ' + result.data, position: 'botton', duration: 3000 }).present();
      });

function on auth-service.ts page

logar(email: string, password: string) {
return new Promise((resolve, reject) => {
  this.http.post(this.API_LOGIN + '&email='+email+'&password='+password)
    .map(res => res.json())
    .subscribe(data => {
        console.log(data);
    },
    (err) => {
      reject(err);
    });
});

}

Return of php

{'success':true, 'data':"00-3A"}

Error that occurs when sending the data to Ionic, I have the return of the webservice and then the error below

 ERROR Error: "Uncaught (in promise): ReferenceError: result is not defined
[47]/HomePage.prototype.realizarLogin/<@http://localhost:8100/build/main.js:395:34
F</l</t.prototype.invoke@http://localhost:8100/build/polyfills.js:3:14974
onInvoke@http://localhost:8100/build/vendor.js:5134:24
F</l</t.prototype.invoke@http://localhost:8100/build/polyfills.js:3:14901
F</c</r.prototype.run@http://localhost:8100/build/polyfills.js:3:10124
f/<@http://localhost:8100/build/polyfills.js:3:20240
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15649
onInvokeTask@http://localhost:8100/build/vendor.js:5125:24
F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15562
F</c</r.prototype.runTask@http://localhost:8100/build/polyfills.js:3:10815
o@http://localhost:8100/build/polyfills.js:3:7887
F</h</e.invokeTask@http://localhost:8100/build/polyfills.js:3:16823
p@http://localhost:8100/build/polyfills.js:2:27646
v@http://localhost:8100/build/polyfills.js:2:27893
  • Missing you call the resolve in the logar nay?

  • I am calling on 2 line (Return new Promise((resolve, Reject) => {)

1 answer

0

I found the problem, as I was accessing another domain I had to add the lines below in a file . htaccess

Header set Access-Control-Allow-Origin "*"

The json was also incorrect, the correct is as below:

{"success":true, "data":"00-3A"}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.