A
A
Alex8062016-06-07 20:30:05
Angular
Alex806, 2016-06-07 20:30:05

How to use Http in Angular 2?

How to work with http in Angular 2?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Golosay, 2016-06-07
@golosay

An example of using a post request in a class, in Angular 2 (typescript) :

import {Http} from '@angular/http';

export class LoginPage {
    public name : string;
    public pass : string;
    constructor(public http: Http) {
        this.http = http //что бы вызывать через this.http, можно эту строку не писать и обращаться напрямую к Http 
    }
    auth() {
        let url = '/conf/post_admin.php';
        let body = "username=" + this.name + "&password=" + this.pass;
        //body должен быть строкой
        this.http.post(url,body)
            .subscribe(data => {
                //где data - респонс от сервера
                console.log(data)
            });
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question