嗨,我刚学会了angular2,我想将下面的代码转换为angular2,你有什么建议吗?
this.postData = function(URL,username,password, ajaxMethod, jsonData, ajaxParams) {
var restURL = URL + ajaxParams;
console.log("Inside ajaxService POST...");
console.log("Connection using URL=[" + restURL + "], Method=[" + ajaxMethod + "]");
var basic = "Basic " + $base64.encode(username + ":" + password);
//alert(basic);
// les requêtes http doivent être toutes authentifiées
var headers = $http.defaults.headers.common;
headers.Authorization = basic;
return $http({
method: ajaxMethod,
url: restURL,
headers: {'Content-Type': 'application/json'},
data: jsonData,
});
};
答案 0 :(得分:0)
private getHeaders(token) {
let headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", "Basic " + token);
let options = new RequestOptions({ headers: headers });
return options;
}
调用标题的方法
getData(): any {
// Get this from different module or service(login service)
let token=this.username+':'this.password;
let headers = this.getHeaders(token);
return this.http.get(this.DataUrl,headers)
.map(res => res.json());
}