0

I have a problem in angular 2 in typescript that browser is not making HTTP Call. I dont see any HTTp call in network section in browser. the angular code works when i dont have any http call in service and it brings data when i use HTTP it doenst make any http request

Following is my code.

<script src="~/lib/es6-shim.min.js"></script>
<script src="~/lib/system-polyfills.js"></script>
<script src="~/lib/shims_for_IE.js"></script>

<script src="~/lib/angular2-polyfills.js"></script>
<script src="~/lib/system.js"></script>
<script src="~/lib/Rx.js"></script>
<script src="~/lib/angular2.dev.js"></script>
<script src="~/lib/http.dev.js"></script>
<script src="~/lib/router.dev.js"></script>

SERVICE :

    import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Observable}     from 'rxjs/Observable';

@Injectable()
export class AuthService {

    private _authPath = 'http://api/Auth';
    constructor(private http: Http) { }
    getAuthSettings() {

        return new Promise((resolve, reject) => {

            return this.http.get(this._authPath).toPromise().then((val: Response) => resolve(val.text()));

        });
    }
    private handleError(error: Response) {
        // in a real world app, we may send the error to some remote logging infrastructure
        // instead of just logging it to the console
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
    }

}

main MODULE :

    import { Component } from 'angular2/core';
import {AuthService} from './auth.service';
import {OnInit} from 'angular2/core';
import {HTTP_PROVIDERS} from 'angular2/http';

@Component({

    selector: "main-app",
    template: "{{title}}",
    providers: [HTTP_PROVIDERS, AuthService]

})
export class AppComponent implements OnInit {
    public title: string = '';
    constructor(private _authService: AuthService) {
    }
    ngOnInit() {
        //this.title = "aaa";
        this._authService.getAuthSettings().then((val: string) => {
            this.title = val;

        });
        //this._authService.getAuthSettings().subscribe((val: string) => this.title = val)
    }

};

1 Answer 1

1

I dont see any HTTP call in network section in browser.

There might be some filters preventing you from seeing it. To be sure update the code as follows:

 console.log('starting');
 this._authService.getAuthSettings()
     .then((val: string) => {
        this.title = val;
        console.log('success');
     },() => console.log('fail'));
 console.log('waiting');

And see which logs happen. Hopefully it helps. Sorry if it doesn't 🌹

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

1 Comment

I am having a similar issue. I set up cors in my asp core app, I call the url with postman and it gives me a json. When I try to call from the service it returns nothing. I tried with your code, and it only shows 'waiting'. stackoverflow.com/questions/40030613/…

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.