Search code examples
angularangular2-templateangular2-services

angular post method parameters


I am currently using angular 4 to build my web application.In that i am getting the data via rest calls to webservices,when i post data i want to know if i can change the body presentation (second param in post method) with an object so the instruction will be easier.

 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": ""
                            }
                          }

) .subscribe( res => { console.log(res); }, err => { console.log("Error occured"); } ); }


Solution

  • You can POST data in Json format like as:

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

    Pay attention on the const data. It is simplest object JS.