0

I am doing a POST request using the HTTP observable and the request is not being made and i am getting no errors, i have a service that defines the method i use to make the request

@Injectable()
export class MovingCompanyService {
constructor(private http: Http){}

registerCompany (body: Object): Observable<RegistrationDetailsModel> {
let bodyString = JSON.stringify(body); // Stringify payload
let headers      = new Headers({ 'Content-Type': 'application/json' }); //      ... Set content type to JSON
let options       = new RequestOptions({ headers: headers }); // Create a   request option

return this.http.post("/moving_company/registration_form_2", body, options) // ...using post request
                    .map((res:Response) => res.json()) // ...and calling     .json() on the response to return data
                    .catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
}   

}

And i have the component that calls this service method

constructor(private router: Router, private moving_company_service: MovingCompanyService) {}
ngOnInit(): void {
    if( Session.has("reg_frm_1")) {
        let frm1Data = Session.get("reg_frm_1");
        console.log("Received data from form 1 ", frm1Data);
    }
}

registerCompany(): void {
    let registrationOperation:Observable<RegistrationDetailsModel>;
    let registration_details = {
        reg_frm_1: Session.get("reg_frm_1"),
        reg_frm_2: this.model
    }
     this.moving_company_service.registerCompany(registration_details)
}

And i am calling the component method 'registerCompany' on a view click event like

<button (click)="registerCompany()">Submit</button>

I am not sure what i am missing or doing wrong, i have imported the rxjs library, the map and and catch operators and have all relevant imports in accordance,i would appreciate your assistance

11
  • whats the error ? Have you checked the network tab did you get entry in network tab ? Also have you checked the url is right where you are posting the data ? Commented Jan 15, 2017 at 9:04
  • 2
    where are you calling subscribe function ? Commented Jan 15, 2017 at 9:05
  • I think u r not calling subscribe on component that is the issue .please have a look on my answer and let me know if it helps Commented Jan 15, 2017 at 9:08
  • The networks tab does not show the request, there is no error in console tab and i am not sure if i know where or how to subscribe to the observable Commented Jan 15, 2017 at 9:11
  • So this means it is not called and As I mentioned in my answer please try that hopefully it will work I had same issues when I started angular2 . without subscribe nothing will happen . Commented Jan 15, 2017 at 9:13

1 Answer 1

1

Try this

        this.moving_company_service.registerCompany
          ((registration_details).subscribe(
            r =>  r,
            error => this.errorMessage = <any>error,
            () => {  });
Sign up to request clarification or add additional context in comments.

1 Comment

I have a string property on my component with name errorMessage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.