How to make AJAX call with angular2(ts)?

前端 未结 4 2068
春和景丽
春和景丽 2020-12-15 07:04

How to make AJAX call with angular2(ts)? I read the tutorial on angularjs.org. But there is nothing about AJAX. So I really want to know how to make AJAX call with angular

4条回答
  •  半阙折子戏
    2020-12-15 07:08

    import { Component, OnInit } from '@angular/core';
    import { Observable } from 'rxjs/Observable';
    
    import { Http, Response, Headers, RequestOptions } from '@angular/http';
    import 'rxjs/add/operator/map';
    
    @Component({
        selector: 'dashboard',
        templateUrl: './dashboard.component.html',
        styleUrls: ['./dashboard.component.css'],
        providers: [RemoteService]
    })
    
    export class DashboardComponent implements OnInit {
        allData = [];
        resu: string;
        errData: string;
        name: string = "Deepak";
    
        constructor(private http: Http){}
    
        ngOnInit(){}
    
        onSubmit(value: any) {
        //console.log(value.message);
        let headers = new Headers({ 'Content-Type': 'application/json'});
        let options = new RequestOptions({ headers: headers });
        let body = JSON.stringify(value);
        this.http.post('127.0.0.1/myProject/insertData.php', body, headers)
                    .subscribe(
                        () => {alert("Success")}, //For Success Response
                        err => {console.error(err)} //For Error Response
                    );                
        }    
    }
    

提交回复
热议问题