如何解决Ionic 2中的Http POST 403(Forbidden)问题

时间:2018-03-13 05:52:35

标签: android ionic-framework ionic2 ionic3 ionic-native

我是Ionic的初学者2.我想从应用程序进行用户注册。对于用户注册,我使用的是METHOD,POST为POST,其媒体类型为x-www-form-urlencoded。我想将数据发送到服务器。

但是当我尝试使用 IONIC服务时,它在网络浏览器上工作正常,但当我尝试从实际设备发送数据时,它会给我 403禁止错误 < / p>

这是我的代码

 doLogins() {
 this.loading = this.loadingController.create({content : "Registering user ..."});
      this.loading.present()
      var url = 'http://services/Registration';

      var officername = this.data.username.trim();
        var officenmae = this.data.office.trim();
        var emal=this.data.email.trim();
        var mobileno=this.data.contact.trim();
        var areode =this.data.areacode;

        var lanlineno=this.data.telno.trim();
        var desgnation= this.data.designation.trim();
        var password=this.data.password.trim();
        var status='A'


     var headers = new Headers()
      headers.append('Content-Type', 'application/x-www-form-urlencoded');

       headers.append('Access-Control-Allow-Origin' , '*');
       headers.append('X-HTTP-Method-Override', 'POST');
       headers.append('Access-Control-Allow-Headers', 'X-Requested-With, X-
     let options = new RequestOptions({ headers: headers });



    let body =  "officname=" +offername + "&emiled=" + email + "&mobilenumber=" + mobileno + "&password=" + password + "&offiname=" + officenmae + 
    "&designation=" + designation +"&landnecode=" + areaode +"&landlinnumber=" + lanlineno +"&status=" + 'A';


           return new Promise(resolve => {
            this.http.post(url, body, options) .map(res => res.json()).subscribe(
                           data => {
                             console.log(data);

                             let toast = this.toastCtrl.create({
                              message: "Registration Success",
                              duration: 3000,
                              position: 'top'
                            });
                            toast.present();
                            setTimeout(() => {
                              this.loading.dismissAll();
                              this.navCtrl.push(LoginPage,{
                                param1: email, param2: password
                            });;
                            }, 2500);

                           },
                           err => {
                             alert(err);
                             this.loading.dismissAll();
                           }
                       );  
         });        
      }

请告诉我我做错了什么。感谢

1 个答案:

答案 0 :(得分:0)

服务器上,在请求的网址的根目录中创建一个包含此内容的“.htaccess”文件:

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Content-Type"