javascript - complex ajax query using parse api -
im dealing parse.com's apis. want make complex queries in javascript function.
i couldnt manage equivalant params section below code ajax request. appriciated.
for example, retrieve scores between 1000 , 3000, gave example in python,
import json,httplib,urllib connection = httplib.httpsconnection('api.parse.com', 443) params = urllib.urlencode({"where":json.dumps({ "score": { "$gte": 1000, "$lte": 3000 } })}) connection.connect() connection.request('get', '/1/classes/gamescore?%s' % params, '', { "x-parse-application-id": "pchvgniu2bbgggmofkcvkquk91g5v3u5g4zgwifk", "x-parse-rest-api-key": "5pi5vb7dnu8mry6ypftotpidpuheqv5gmchfaqxk" }) result = json.loads(connection.getresponse().read()) print result
the code in javascript can shorter:
$.ajax({ type:"get", beforesend: function (request) { request.setrequestheader("x-parse-application-id", 'pchvgniu2bbgggmofkcvkquk91g5v3u5g4zgwifk'); request.setrequestheader("x-parse-rest-api-key", '5pi5vb7dnu8mry6ypftotpidpuheqv5gmchfaqxk'); }, url: "https://api.parse.com/1/classes/gamescore", data: "where=" + escape(json.stringify({"score": {"$gte": 1000, "$lte": 3000 }})), processdata: false, success: function(msg) { console.log(json.parse(msg)); } });
jquery append data url query string, final request url be:
https://api.parse.com//1/classes/gamescore?where=%7b%22score%22%3a%7b%22%24gte%22%3a1000%2c%22%24lte%22%3a3000%7d%7d
and of course it's ok construct url ourselves:
var url = "https://api.parse.com/1/classes/gamescore?where=" + escape(json.stringify({"score": {"$gte": 1000, "$lte": 3000 }})) $.ajax({ type:"get", beforesend: function (request) { request.setrequestheader("x-parse-application-id", 'pchvgniu2bbgggmofkcvkquk91g5v3u5g4zgwifk'); request.setrequestheader("x-parse-rest-api-key", '5pi5vb7dnu8mry6ypftotpidpuheqv5gmchfaqxk'); }, url: url, processdata: false, success: function(msg) { console.log(json.parse(msg)); }
});
the request url same previous one.
Comments
Post a Comment