我需要使用AngularJS的$http调用Parse RESTFUL API来检索特定的数据,Parse允许我这样做的唯一方法是将"——data-urlencode where={"storeId":2}"发送到curl调用中。
curl -X GET -H "X-Parse-Application-Id:ApplicationId" -H "X-Parse-REST-API-Key: ApiKey" -G --data-urlencode 'where={"storeId":2}' https://api.parse.com/1/classes/Stores/
见这里:https://parse.com/docs/rest#queries-arrays
我能够使用终端返回JSON结果,但不能使用$http()。有人知道我如何注入"——data-urlencode where={"storeId":2}"到AngularJS $http()或$resource()方法?
请看这里:http://docs.angularjs.org/api/ng.$http
查看解析api的python引用,我猜where={"storeId":2}
可以作为查询参数附加(如果不访问解析api,我无法对此进行测试)。参考文献:
params = urllib.urlencode({"where":json.dumps({
"arrayKey": 2
})})
在AngularJS中,你可以用param
键在$http
的config
对象中添加查询参数:
var req = $http({
params: {
where: {storeId: 2},
},
headers: {
'X-Parse-Application-Id': 'ApplicationID',
'X-Parse-REST-API-Key': 'ApiKey'
}
//... other params, like URL
});
params.where
值将自动转换为JSON字符串。我不确定查询参数是否默认得到URL编码,如果不是,JSON手动编码它们并应用encodeURIComponent
函数。