我是 Ionic 2 的初学者。我想从应用程序进行用户注册。对于用户注册,我正在使用api,该方法是POST,其媒体类型是x-www-form-urlencoded。我想将数据发送到服务器,但无法成功发送请求。
请我需要一些帮助。[![在此输入图像描述][1]][1]
在我的POST请求中有9 个参数它们是:
1.官员姓名 2.办公室名称 3.指定 4.电子邮件5.座机代码6.座机号码 7.手机号码8.密码9.状态
这是我的代码
doLogin() {
let url = "http:////Registration";
var headers = new Headers()
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let body= JSON.stringify({ officme: 'mr',emled:'adminac.com', mobilenumber:'9025225',
password:'12345',offm:'mrc',desigtion:'prrammer',landlinecode:'0000',
landlinenumber:'888888',status:'A'});
alert(body);
return this.http.post(url, body, options) .map(res => res.json()).subscribe(
data => {
console.log(data);
},
err => {
console.log("ERROR!: ", err);
}
);
}
}
请替换此代码,我希望它能根据您的要求工作
doLogin() {
let url = "http:////Registration";
var headers = new Headers()
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let body = "offiername=" +'mr' + "&emailed=" + 'admin@mac.com' + "&mobilenumber=" + '9025225' + "&password=" + '12345' + "&officename=" + 'mrc' + "&designation=" + 'programmer' +"&landlnecode=" + '0000' +"&landlinenumber=" + '888888' +"&sttus=" + 'A';
alert(body);
return this.http.post(url, body, options) .map(res => res.json()).subscribe(
data => {
console.log(data);
},
err => {
console.log("ERROR!: ", err);
}
);
}
}
尝试以下代码:
let url = "http://host//Registration";
let headers = new Headers();
headers.append('Access-Control-Allow-Origin' , '*');
headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
headers.append('Accept','application/json');
headers.append('content-type','application/json');
let options = new RequestOptions({ headers:headers});
return new Promise((resolve,reject)=>{
let body= JSON.stringify({ officername: 'mrsac',emailed:'admin@mrc.com', mobilenumber:'902160',password:'12345',officename:'mr',designation:'programmer',landlinecode:'0000',
landlinenumber:'888888',status:'A'});
this.http.post(url,JSON.stringify(body), options).subscribe(res => {
resolve(res.json());
}, (err) => {
reject(err);
});
})