2017-10-09 70 views
0

我目前正在使用角度4來構建我的web應用程序。在那裏,我通過對web服務的其餘調用來獲取數據,當我發佈數據時我想知道是否可以更改主體表示(第二個參數在後期的方法)與對象,所以說明會更容易。角度發佈方法參數

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": "[email protected]", 
           "enabled": true, 
           "lastName": "waaaa", 
           "login": "[email protected]", 
           "name": "souad12219", 
           "phone": "" 
          } 
          } 

) .subscribe( 解析度=> { 的console.log(RES); }, ERR => { 的console.log( 「發生錯誤」);} ); }

回答

0

可以JSON格式的POST數據,如爲:

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); 
    } 

注重對const data。這是最簡單的對象JS。

+0

我忘了標題選項 – fbm

+0

請接受我的回答 – OPV

+0

已接受 – fbm

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": "[email protected]", 
           "enabled": true, 
           "lastName": "waaaa", 
           "login": "[email protected]", 
           "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"); }); 
} 
+0

我收到此錯誤內容類型text/plain不支持:/ – fbm