以前ng1會用 $httpParamSerializerJQLike 來處理要 post 的資料(json),
到了ng2/3, 要用 URLSearchParams,是要一個個 variable 的 set 入去,
請問除了自己起個 loop function外,有沒有其他方法?
另外,我見到網上大部份例子都使用簡單的method1,
可是我用 codeigniter rest api 架 api 只有處理 POST, 沒有OPTION (return 405)
是否 API 那邊設定得不好?
//NG1
var param = {
email: 'aa@aa.com',
password: '1234'
};
$http({
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
,method: 'POST'
,url: 'https://a.com/login'
,data: $httpParamSerializerJQLike(param)
,timeout: 10000
,responseType: 'json'
}).then(function successCallback(response) {
//success
}}, function errorCallback(response) {
//fail
});
}
//NG3
public login_method1():void {
const API_URL:string = 'https://a.com/login';
var param = {
email: 'aa@aa.com',
password: '1234'
};
this.http.post(API_URL, param)
.map(res => res.json())
.subscribe(data => {
//ng changed into OPTION requests, server return status 405 return
}, error => {
console.log("Oooops!");
});
}
public login_method2():void {
const API_URL:string = 'https://a.com/login';
const API_HEADERS = new Headers({'Content-Type': 'application/x-www-form-urlencoded'});
const API_OPTIONS = new RequestOptions({ headers: API_HEADERS});
let param = new URLSearchParams();
param.set('email', "aa@aa.com");
param.set('password', "1234");
this.http.post(API_URL, param, API_OPTIONS)
.map(res => res.json())
.subscribe(data => {
// RETURN SUCCESS
}, error => {
console.log("Oooops!");
});
}