This works fine on a browser but not in a capacitor app on an android device, ios not tested…
I wonder if this has to do with capacitor or something else. Hope someone can help me with this. Thanks in advance!
senduserdata(username){
var dataToSend = {
username:this.Username,
password:this.Password,
usertype:this.getSelectedSubject,
}
var url = 'https://myappp15.herokuapp.com/login';
this.http.post(url,{data:JSON.stringify(dataToSend)},{responseType: 'text'}).subscribe(
(data)=>{
alert(data);
this.message = data;
if(data === "Logged In Successfully!")
{
this.LoginCustomer();
this.cartservice.setUsernameCustomer(this.Username);
}
else if(data === "Welcome!")
{
this.LoginStaff();
this.cartservice.setUsernameStaff(this.Username);
}
}
)
}
I would recommend to use the IonAlert
It seems that alerts work, it’s the this.http.post
that doesn’t work on mobile…
I’m trying to do the same http.post request with ionic advance HTTP but not sure how to convert it from angular http to ionic http
could u help me with that…
It has to be something like
return from (this.http.post(url,{data:JSON.stringify(dataToSend)},{responseType: 'text'}) .pipe(map((data: any) => JSON.parse(data?.data)))
I think it’s a mistake to overengineer HTTP. Get it working with:
- Angular
HttpClient
- never manually calling
stringify
or setting Content-Type
headers
- no
any
If there are CORS problems, fix them on the server side instead of kludging around them on the client side.
Should I consider that there are CORS problems if I do get a response from get requests but not from post requests? If yes how to fix it?
The code in the Angular HttpClient
works fine in the Browser… What should I change from the code in the question on the top to get it to work in android also?
Turns out @rapropos was right, there were CORS problems that I fixed but adding the code below on my node.js file
var app = express();
var cors = require('cors');
app.use(cors())
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();
});