本文整理汇总了TypeScript中@angular/http.Http.method方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Http.method方法的具体用法?TypeScript Http.method怎么用?TypeScript Http.method使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@angular/http.Http
的用法示例。
在下文中一共展示了Http.method方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: makeRequest
makeRequest(method: string, uri: string, params: Object, body:Object = null) {
// build query string
let queryParams = new URLSearchParams();
// build query
params = this.generateRequestQuery(uri, method, params, body);
// url-ify
Object.keys(params).forEach(k => {
this.log(k, {});
if (params[k] !== undefined) {
queryParams.set(k, (typeof params[k] !== 'string' ? JSON.stringify(params[k]) : params[k]).replace(/\{/g, '%7B').replace(/\}/g, '%7D'));
}
});
// Create request options
let options:RequestOptions = this.generateRequestOptions(uri, method, params, body);
// Add query string
options.search = queryParams;
this.log('Performing request with the following options', options);
// Switch api based on method
switch(method) {
case 'get':
case 'delete':
return this.http[method](uri, options);
default:
return this.http[method](uri, JSON.stringify(body), options);
}
}
示例2: request
private request(method: string, path: string, payload?) {
let body = null;
let headers = null
let options = null;
if (payload) {
body = JSON.stringify(payload);
headers = new Headers({ 'Content-Type': 'application/json' });
options = new RequestOptions({ headers: headers });
}
return this.http[method](path, body, options).map((res) => res.json()).catch(this.handleError);
}
示例3: request
request(method, url, data = null) {
let token = JwtToken.get();
let options:RequestOptionsArgs = {
"headers": new Headers({
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: token
})
};
let finalUrl = `${Config.API_URL}${url}`;
let query;
switch (method) {
case 'get':
options.search = this._mapToURLSearchParams(data);
query = this.http.get(finalUrl, options);
break;
case 'delete':
query = this.http.delete(finalUrl, options);
break;
default:
query = this.http[method](finalUrl, JSON.stringify(data), options);
break;
}
return query.catch((response) => {
//error handling
//noinspection TypeScriptUnresolvedFunction
let text = response.text();
let error = null;
try {
error = JSON.parse(text);
error = error.error;
}
finally {
}
if (error) {
throw error;
} else {
throw response;
}
}).map(result => {
let text = result.text();
//e.g. empty result can be returned for "delete"
if (text === '') {
return {};
}
//noinspection TypeScriptUnresolvedFunction
return result.json()
})
.map(ApiService.proceedDates)
}
示例4: addDepartament
addDepartament(dep:Departament, _method:any): Promise<Departament[]> {
let headers = new Headers({
'Content-Type': 'application/json'});
let method = 'post';
if(_method == "modify"){
method = 'put';
}
return this.http[method]('http://0.0.0.0:3000/api/Departments',JSON.stringify(dep), {headers: headers})
.toPromise()
.then(response =>
response.json())
.catch(this.handleError);
}
示例5: save
save(app: Appointment): Observable<Appointment> {
let body = JSON.stringify(app);
let token = sessionStorage.getItem('token');
let headers = new Headers();
headers.append('Authorization', 'Bearer ' + token);
let url = 'http://api.marabinigomme.it/appointments';
let method = 'put';
if (app.id) {
url = url + '/' + app.id;
method = 'post';
}
return this.http[method](url, body, new RequestOptions({headers: headers}))
.map(this.extractData)
.catch(this.handleError);
}
示例6: load_json
private load_json(method, uri, params, resolve) {
let resolved = false;
this._http[method](uri, { search: params }).subscribe(
res => {
try {
resolve(res.json());
resolved = true;
} catch (e) {
console.error("Failed jsoning", res, resolved);
}
},
error => setTimeout(() => {
console.error("HTTP Error", error);
this.load_json(method, uri, params, resolve);
}, 1000),
() => {
if (!resolved) {
console.error("Not resolved but completed");
this.load_json(method, uri, params, resolve);
}
});
}
示例7: makeRequest
private makeRequest(method: string, requestBody?: string): Observable<any> {
return this._http[method](Config.apiUrl + "Groceries",
{ headers: this.header }, requestBody)
.map(res => res.json())
.catch(this.handleErrors);
}
示例8: load_text
private load_text(method, uri, params, resolve) {
this._http[method](uri, { search: params })
.subscribe(res => resolve(res), error => setTimeout(() =>
this.load_text(method, uri, params, resolve), 1000));
}
示例9: rawRequest
rawRequest(method: string, route: string, params: Object, body:Object) {
return this.http[method](`${StoreService.config.baseUri}/${route}`, params, body);
}
示例10: requestVoid
private requestVoid(url: string, data: {}, method: string = 'post'): Observable<void> {
return this.http[method](url, data).map(response => {}).catch(this.handleError);
}