我正在尝试在 ionic 2 应用程序中转换 ionic 应用程序,并且正在努力尝试重新制作登录进度。我在我的 ionic 2 应用程序中写了这个:
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);
});
}
在 ionic 1 应用程序中,这是代码:
$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 = [];
for (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'
},
}
我的问题在 POST 中,当我使用 ionic 2 应用程序的代码发布时,这是我的表单数据的样子:
{
"jsonLogin": "{"UserID":"admin","CompanyID":"test","Password":"pass"}"
}:
虽然 ionic 1 应用程序表单数据如下所示:
jsonLogin:{"UserID":"admin","CompanyID":"test","Password":"pass"}
自从我从服务器收到错误消息以来,开机自检正在工作,我只是不知道如何格式化数据以执行正确的开机自检。
提前感谢您的帮助。
编辑:通过添加新标头使其工作:
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*/*'
});
并发送一个硬编码字符串:
createStringForLogin(username: any, company: any, password: any){
return 'jsonLogin={"UserID":"'+username+'","CompanyID":"'+company+'","Password":"'+password+'"}';
}
我认为
let body = {"jsonLogin": this.loginData};
应该解决你的问题。如果它不起作用,请告诉我。
此外,如果您在将参数设置为this.loginData
.像这样设置它们:
this.loginData = {};
this.loginData['UserID'] = this.username;
this.loginData['CompanyID'] = this.company;
this.loginData['Password'] = this.password;