Ionic 2 http.post to login -
i'm trying convert ionic app in ionic 2 app , im strugle trying remake login progress. wrote in ionic 2 app:
loginuser(){ localstorage.setitem('username', this.username); localstorage.setitem('password', this.password); localstorage.setitem('company', this.company); this.logindata = {}; this.logindata.userid = this.username; this.logindata.companyid = this.company; this.logindata.password = this.password; let body = {"jsonlogin": json.stringify(this.logindata)} let link = 'working link'; let headers = new headers({ 'content-type': 'application/x-www-form-urlencoded; charset=utf-8' }); let options = new requestoptions({headers: headers}); this.http .post(link, body, options) .map(res => res.json()) .subscribe( data => { console.log(data); //this.navctrl.push(menupage); }, err => { console.log(err); }); }
while in ionic 1 app code:
$scope.loginuser = function () { json = {}; json.userid = $scope.data.username; json.companyid = $scope.data.company; json.password = $scope.data.password; $http({ method: 'post', data: { "jsonlogin": json.stringify(json) }, url: 'working link', transformrequest: function (obj) { var str = []; (var p in obj) str.push(encodeuricomponent(p) + "=" + encodeuricomponent(obj[p])); return str.join("&"); }, headers: { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8' }, }
my problem in post, when post code of ionic 2 app form data looks like:
{ "jsonlogin": "{\"userid\":\"admin\",\"companyid\":\"test\",\"password\":\"pass\"}" }:
while ionic 1 app form data looks this:
jsonlogin:{"userid":"admin","companyid":"test","password":"pass"}
the post working since got error message server, don't know how can format data correct post.
thank in advance help.
edit: got working added new header:
let headers = new headers({ 'content-type': 'application/x-www-form-urlencoded', 'accept': '*/*' });
and send hardcoded string:
createstringforlogin(username: any, company: any, password: any){ return 'jsonlogin={"userid":"'+username+'","companyid":"'+company+'","password":"'+password+'"}'; }
i think
let body = {"jsonlogin": this.logindata};
should solve problem. let me know if not work.
also, if having trouble in setting parameters this.logindata
. set them this:
this.logindata = {}; this.logindata['userid'] = this.username; this.logindata['companyid'] = this.company; this.logindata['password'] = this.password;
Comments
Post a Comment