1

I am trying to test my application written in Ionic 2 framework on actual device. The problem is that whenever I try to call my node.js API from device, the call does not even get there. Everything works fine on computer with ionic server command, but I cannot run it on my phone when I use ionic cordova run android. I can run sample applications that do not use my API with no errors.

My API code that allows any origin is following:

app.all('/*', function(req, res, next) { 
   var origin = req.headers.origin;

   res.setHeader('Access-Control-Allow-Origin', origin);
   res.header("Access-Control-Allow-Credentials", "true");
   res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
   res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
   next();
});

And my code on client side that calls API is:

  checkLogin(): Observable<boolean>{
     return this.http
    .get(`http://localhost:8080/user/loginstatus`, { headers: this.getHeaders(), withCredentials: true})
    .map((res: Response) => {
   if(res.json()==true){
     return true;
   }
   else{
     return false;
   }
  });
 }

Basicly I would like to check if user is logged in so I can set the proper root page in the beggining of the app start. Please help. Thank you in beforehand.

Luki
  • 409
  • 11
  • 25
  • did you include meta security content in index.html file – Sathiyaraj May 24 '17 at 07:14
  • 1
    When using the device, your client's `localhost` is not your server's `localhost`. – cartant May 24 '17 at 07:14
  • I edited that... I was not using that array with allowed origins. I simply allow every origin with the written code.. And yes I did include meta security content in my index.html file. I tried everything that this answer says: https://stackoverflow.com/questions/30326148/cordova-ionic-http-request-not-processing-while-emulating-or-running-on-dev – Luki May 24 '17 at 07:17
  • @cartant so is there the problem with my url when calling get method? What url should I use? – Luki May 24 '17 at 07:19
  • 1
    Whatever is appropriate given your network setup. On the device, `localhost` is the device - not the server. – cartant May 24 '17 at 07:21
  • So I should check my computer's ip using ipconfig command and replace localhost with ipv4 address? – Luki May 24 '17 at 07:23
  • It depends. Are they on the same network? If so, yeah, using an IP address should be fine. – cartant May 24 '17 at 07:28
  • I tried this, but still no success... I think I am doing something wrong with IP address I am trying to access... – Luki May 24 '17 at 07:32
  • keep those services in a temporary/free hosting websites and try.. – varun aaruru May 24 '17 at 07:58

0 Answers0