0
import {Injectable} from 'angular2/core';
import {SampleInfo} from 'app/sample-info/model/sample-info-model';
import {HTTP_PROVIDERS, Http, Response, Headers, RequestOptions} from 'angular2/http';

import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';


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

  private _url = 'http://localhost:9090/sample/details/1213';

  getInfo () {
    headers = new Headers({ 'Authorization': 'Basic AVBDIEtr009=' });
    options = new RequestOptions({ headers: headers });
    // $.ajax(this._url); 
    return this.http.get(this._url, options)
                    .map(res => <SampleInfo> res.json())
                    .do(data => console.log(data))
                    .catch(this.handleError);
  }

  private handleError (error: Response) {
    console.error(error);
    return Observable.throw(error.json().error || 'Server error');
  }
}

Please check the above code snippet. In that, this.http.get(...) is not sending the request to the url, but $.ajax(...) is working, could someone please help ?

i already call this from the component class which use the service,

import {Component, OnInit} from 'angular2/core';
import {Router} from 'angular2/router';
import {SampleInfoService} from 'app/booking-info/service/sample-info-service';
import {SampleInfoModel} from 'app/booking-info/model/sample-info-model';
import {TranslateService, TranslatePipe} from 'ng2-translate/ng2-translate';
import {HTTP_PROVIDERS} from 'angular2/http';


@Component({
  selector: 'sample-information',
  templateUrl: 'app/sample-info/templates/sample-info.html',
  providers: [SampleInfoService],
  pipes: [TranslatePipe]

})
export class SampleInfoComponent implements OnInit {
    public sampleInformation = {};
    constructor(private _sampleInfoService: SampleInfoService,private _translate: TranslateService) {
            this.initializeTranslateServiceConfig(_translate, 'en')
        }

    getSampleInfo() {
        this._sampleInfoService.getInfo()
                     .subscribe(
                       SampleInfo => {
                            this.sampleInformation = SampleInfo;
                            console.log(this.sampleInformation);
                        },
                       error =>  this.errorMessage = <any>error);
    }

1 Answer 1

4

update I assume you try to access this.sampleInformation before the value has been received.

Observables are async and getInfo().subscribes() returns before SampleInfo => this.sampleInformation = SampleInfo is executed. This is only executed sometimes later when the response from the server arrives;

Try

getSampleInfo() {
    this._sampleInfoService.getInfo()
                 .subscribe(
                   SampleInfo => {
                     this.sampleInformation = SampleInfo;
                     console.log(this.sampleInformation);
                   },
                   error =>  this.errorMessage = <any>error);
}

original

Observables are lazy by default. You need to this.getinfo().subscribe(x => {}) to initiate the request.

Sign up to request clarification or add additional context in comments.

13 Comments

Obviously, your question doesn't provide enough information.
Thanks for looking into this @günter-zöchbauer, i have edited the question, please have a look.
I updated my answer. Please check in the browser console if you see the request being actually made.
That is the actual issue, the request is never actually going, i checked browser console, with $.ajax(...), the request is going.
I'm out of ideas. Can you create a Plunker to allow to reproduce the problem? You can use plnkr.co/edit/PClI775hEI6iijOIxExO?p as template.
|

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.