【问题标题】:How to cURL in Angular 2?如何在 Angular 2 中卷曲?
【发布时间】:2016-04-19 14:39:45
【问题描述】:

我正在使用 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;
});

【问题讨论】:

    标签: angular curl parse-platform angular2-http


    【解决方案1】:

    您应该查看URLSearchParams API,它将帮助您形成查询字符串。在使用它之前,请从 angular2/http 导入 URLSearchParams

    代码

    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;
    });
    

    【讨论】:

    • @JamesRunner 很高兴知道这一点。谢谢:-)
    猜你喜欢
    • 2016-12-06
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多