complex ajax query using parse api
Question: Im dealing with parse.com's apis. I want to make complex queries in my
javascript function.

I couldnt manage to equivalant params section below code for ajax get request.
Any help appriciated.

For example, to retrieve scores between 1000 and 3000, they 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
    


Answer: The code in javascript can be even 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 will append data to url as query string, the final request url will 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 to construct the url by 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 is the same as the previous one.

