Skip to content
Advertisement

Ionic/Angular HTTP post request not working on android

I used Angular HTTP while testing in the browser and it worked fine, but it doesn’t on an actual mobile device… Apparently HTTP angular doesn’t work on mobile and I had to convert the post request to ionic native HTTP. I’m not sure if it’s converted correctly and what the issue is…
Also, the get requests work fine is only the post requests that don’t work.
Hope someone can help me with this.
Thanks in advance!

My code :
angular HTTP post request

senduserdata(username){
    var dataToSend = {
      username:this.Username,
      password:this.Password,
      usertype:this.getSelectedSubject,
    }

    var url = 'https://mylink.herokuapp.com/login';

    this.http.post(url,{data:JSON.stringify(dataToSend)},{responseType: 'text'}).subscribe(
      (data)=>{
        alert(data);
        if(data === "Logged In Successfully!")
        {
          this.LoginCustomer();
          this.cartservice.setUsernameCustomer(this.Username);
        }
        else if(data === "Welcome!")
        {
          this.LoginStaff();
          this.cartservice.setUsernameStaff(this.Username);
        }
      }
    ) 
  }

ionic advanced HTTP post request

senduserdata(){
    var dataToSend = {
      username:this.Username,
      password:this.Password,
      usertype:this.getSelectedSubject,
    }

    var url = 'https://mylink.herokuapp.com/login';

    this.http.post(url,{data:JSON.stringify(dataToSend)},{responseType: 'text'}).then(
      (data)=>{
        this.message= JSON.parse(data.data);
        alert(this.message)
    
        if(this.message === "Logged In Successfully!")
        {
          this.LoginCustomer();
          this.cartservice.setUsernameCustomer(this.Username);
        }
        else if(this.message === "Welcome!")
        {
          this.LoginStaff();
          this.cartservice.setUsernameStaff(this.Username);
        }
      }
    ) 
  }

Advertisement

Answer

Update
Turns out it works on the browsers cause I was using CORS changer extensions,
I just had to add in my node.js file

app.all('*', function(req, res, next) {
    var origin = req.get('origin'); 
    res.header('Access-Control-Allow-Origin', origin);
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
});

var app = express();
var cors = require('cors');
app.use(cors())
Advertisement