Http
Experimental Class
Обзор класса
class Http {
constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions)
request(url: string|Request, options?: RequestOptionsArgs) : Observable<Response>
get(url: string, options?: RequestOptionsArgs) : Observable<Response>
post(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response>
put(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response>
delete(url: string, options?: RequestOptionsArgs) : Observable<Response>
patch(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response>
head(url: string, options?: RequestOptionsArgs) : Observable<Response>
options(url: string, options?: RequestOptionsArgs) : Observable<Response>
}
Описание класса
Выполняет HTTP-запросы, используя XMLHttpRequest в качестве стандартного бэкенда.
Http доступен как инжектируемый класс с методами для выполнения HTTP-запросов. Вызов request возвращает Observable, который будет издавать один Response при получении ответа.
Пример
import {Http, HTTP_PROVIDERS} from '@angular/http';
import 'rxjs/add/operator/map'
@Component({
selector: 'http-app',
viewProviders: [HTTP_PROVIDERS],
templateUrl: 'people.html'
})
class PeopleComponent {
constructor(http: Http) {
http.get('people.json')
// Call map on the response observable to get the parsed people object
.map(res => res.json())
// Subscribe to the observable to get the parsed people object and attach it to the
// component
.subscribe(people => this.people = people);
}
}
Пример
http.get('people.json').subscribe((res:Response) => this.people = res.json());
Стандартный конструктор для выполнения запросов, XMLHttpRequest, абстрагирован как «бэкенд» ( XHRBackend в данном случае), который можно смоделировать с помощью инъекции зависимостей, заменив поставщик XHRBackend, как показано в следующем примере:
Пример
import {BaseRequestOptions, Http} from '@angular/http';
import {MockBackend} from '@angular/http/testing';
var injector = Injector.resolveAndCreate([
BaseRequestOptions,
MockBackend,
{provide: Http, useFactory:
function(backend, defaultOptions) {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]}
]);
var http = injector.get(Http);
http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));
Аннотации
@Injectable()
Конструктор
constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions)
Подробное описание класса
request(url: string|Request, options?: RequestOptionsArgs) : Observable<Response>
Выполняет запросы любого типа HTTP. Первый аргумент обязателен и может быть URL или экземпляром Request. Если первый аргумент — URL, в качестве второго аргумента можно передать необязательный объект RequestOptions. Параметры объекта будут объединены со значениями BaseRequestOptions перед выполнением запроса.
get(url: string, options?: RequestOptionsArgs) : Observable<Response>
Выполняет запрос с методом HTTP get.
post(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response>
Выполняет запрос с методом HTTP post.
put(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response>
Выполняет запрос с методом HTTP put.
delete(url: string, options?: RequestOptionsArgs) : Observable<Response>
Выполняет запрос с методом HTTP delete.
patch(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response>
Выполняет запрос с методом HTTP patch.
head(url: string, options?: RequestOptionsArgs) : Observable<Response>
Выполняет запрос с методом HTTP head.
options(url: string, options?: RequestOptionsArgs) : Observable<Response>
Выполняет запрос с методом HTTP options.
экспортировано из @angular/http/index, определено в @angular/http/src/http.ts
© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v2.angular.io/docs/ts/latest/api/http/index/Http-class.html