我是 Ionic 2 的初学者。我想从应用程序进行用户注册。对于用户注册,我正在使用API,该方法是POST,其媒体类型是x-www-form-urlencoded。我想将数据发送到服务器。
但是当我尝试使用 IONIC 服务时,它在网络浏览器上工作正常,但是当我尝试从实际设备发送数据时,它会给我 403 禁止的错误
这是我的代码
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();
}
);
});
}
请告诉我我做错的地方。谢谢
在您的服务器上,在请求的 URL 的根目录中创建一个包含以下内容的".htaccess"文件:
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "Content-Type"