简体   繁体   English

离子/角 HTTP 发布请求不适用于 android

[英]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 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 angular HTTP 发布请求

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离子高级 HTTP 发布请求

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);
        }
      }
    ) 
  }

Update更新
Turns out it works on the browsers cause I was using CORS changer extensions,原来它适用于浏览器,因为我使用的是 CORS 转换器扩展,
I just had to add in my node.js file我只需要添加我的 node.js 文件

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM