如何在 Angular 2 中卷曲?

How to cURL in Angular 2?

我正在使用 Parse REST API 使用 Angular2 获取一些数据,一切正常,但我需要使用 降序 获取数据,返回 Parse 文档应执行以下操作:

curl -X GET \
-H "X-Parse-Application-Id: appID" \
-H "X-Parse-REST-API-Key: keyID" \
-G \
--data-urlencode 'order=-score' \
https://api.parse.com/1/classes/GameScore

我想在我的 Angular2 代码中模拟那个 cURL,我主要需要实现的是:data-urlencode

目前,这是我的 Angular2 代码:

this.http.get('https://example.com/classes/GameScore').subscribe(data => {
    this.res = data.json().results;
});

你应该看看 URLSearchParams API 这将帮助你形成查询字符串。在使用它之前做 import URLSearchParams from angular2/http

代码

import {URLSearchParams} from 'angular2/http'; //do add this line

var search = new URLSearchParams()
search.set('order', '-score');

this.http.get('https://example.com/classes/GameScore', { search }).subscribe(data => {
    this.res = data.json().results;
});