我在Angular2上使用了,但出现了以下警告消息。
不推荐主线程上的同步XMLHttpRequest,因为它会对最终用户的体验产生有害影响。有关更多帮助,请查看
请告诉我怎么做。
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),
);
}
}发布于 2017-03-20 07:56:24
为什么不能将内容类型传递为“application/json”格式,
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());https://stackoverflow.com/questions/42897336
复制相似问题