我目前正在使用角度4来构建我的web应用程序。在我通过其他调用webservices获取数据时,当我发布数据时我想知道我是否可以更改正文表示(post方法中的第二个参数)用一个对象使指令更容易。
updatePasswordWithObservable(userToAdd:User) {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post(this.updatePwdUrl,
{
"internalId": 0,
"oldPwd": "",
"pwd": "123",
"reset": true,
"userEntry": {
"activeNotifications": true,
"aspects": [
{
"aspectName": "string",
"internalId": 0
}
],
"email": "souad122@gmail.com",
"enabled": true,
"lastName": "waaaa",
"login": "souad122@gmail.com",
"name": "souad12219",
"phone": ""
}
}
) 。订阅( res => { 的console.log(RES); }, err => { console.log(“出错”); } ); }
答案 0 :(得分:1)
是的,您可以使用对象。但是,您需要先将其转换为JSON
。您可以使用JSON.stringify
执行此操作:
updatePasswordWithObservable(userToAdd:User) {
let data = JSON.stringify({
"internalId": 0,
"oldPwd": "",
"pwd": "123",
"reset": true,
"userEntry": {
"activeNotifications": true,
"aspects": [
{
"aspectName": "string",
"internalId": 0
}
],
"email": "souad122@gmail.com",
"enabled": true,
"lastName": "waaaa",
"login": "souad122@gmail.com",
"name": "souad12219",
"phone": ""
}
});
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post(this.updatePwdUrl, data)
.subscribe( res => { console.log(res); }, err => { console.log("Error occured"); } );
}
答案 1 :(得分:0)
您可以使用Json格式发布数据,如:
library(dplyr)
col_names <- names(df %>% select(contains("Sep")))
df[,col_names] = df[,col_names]*0.5
关注public loadApp(): Observable<any> {
const data = {
'type' : 'ReadRequest',
'query' : 'get',
'parameters' : { 'app' : 'new' }
};
return this.http.post(null, data)
.map(res => (<any>res)._body === '' ? {} : res.json())
.catch(this.handleError);
}
private handleErrorObservable(error: Response | any) {
return Observable.throw(error.message || error);
}
private handleError(error: any): Promise<any> {
console.error('loadAuditories: ', error);
return Promise.reject(error.message || 'Server error: ' + error);
}
。它是最简单的对象JS。