Angular2在chrome处出现警告XMLHttpRequest

时间:2017-03-20 06:45:24

标签: rest angular http xmlhttprequest

我在Angular2上使用了Http Restful API,但出现了以下警告信息。

主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用。如需更多帮助,请查看https://xhr.spec.whatwg.org/

请告诉我该怎么做。

<小时/> 的 http_restful_service.ts

import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';

@Injectable()

export class HTTPRestfulService {

  constructor(private _http: Http) {}

  getAllProjectName() {
    var headers = new Headers();
    headers.append('Content-Type','charset=uft-8');
    return         
       this._http.get('http://localhost/api/database/',
                     {headers: headers})
                 .map(res => res.json());
  }
}

backstage_view.component.ts

import { Component, OnInit } from '@angular/core';
import { HTTPRestfulService } from './../../../service/http_restful_service';

@Component({
    moduleId: module.id,
    selector: 'backstage_view',
    templateUrl: './backstage_view.component.html',
    styleUrls: ['./backstage_view.component.css']
})

export class BackstageViewComponent implements OnInit {

    allProjects: string;

    constructor(private _restfulapi: HTTPRestfulService) {}

    ngOnInit() {     
      this._restfulapi.getAllProjectName()
                      .subscribe(
                        data => this.allProjects = data,
                        error => console.log(error),
                      );
    }
}

1 个答案:

答案 0 :(得分:0)

为什么你不能将内容类型作为&#39; application / json&#39;格式,

let url= `http://localhost/api/database/`;
    let headers = new Headers();
    headers.append('Content-Type','application/json');
    let params = new URLSearchParams;
    params.append('id', id);
    params.append('user_id', user_id);
    return this.authHttp.post( url,  { headers:headers,  search:params })
   .map(res => res.json());