admin管理员组

文章数量:1415645

I am making an asynchronous 'GET' request using the Parse API. The AJAX request returns an array of strings that may not be in the right order. I have searched on Google for an 'order' property AJAX may possess but no luck.

This is my AJAX request as a function called retrieve getting the value of the text property.

AJAX Request:

Obj.retrieve = function(callBack) {
   $.ajax({
       url : '',
       type : 'GET',
       dataType: 'JSON',
       contentType : 'application/json',
       data : JSON.stringify({   
           text : 'value: ',
           order: "-createdAt"          // Would the order property go here?        
       }),
       error : function(data) {
           console.log('error: ' + data);
       },
       success : function(data) {
          for (var i = 0; i < data["results"].length; i++)
             callBack(data["results"][i].text);
       }
   });
}; 

TL;DR: Each object has field names createdAt, updatedAt, and objectId thanks to Parse. Ideally, I would like to use createdAt with the order. Any suggestions?

I am making an asynchronous 'GET' request using the Parse API. The AJAX request returns an array of strings that may not be in the right order. I have searched on Google for an 'order' property AJAX may possess but no luck.

This is my AJAX request as a function called retrieve getting the value of the text property.

AJAX Request:

Obj.retrieve = function(callBack) {
   $.ajax({
       url : 'https://api.parse./1/classes/messages',
       type : 'GET',
       dataType: 'JSON',
       contentType : 'application/json',
       data : JSON.stringify({   
           text : 'value: ',
           order: "-createdAt"          // Would the order property go here?        
       }),
       error : function(data) {
           console.log('error: ' + data);
       },
       success : function(data) {
          for (var i = 0; i < data["results"].length; i++)
             callBack(data["results"][i].text);
       }
   });
}; 

TL;DR: Each object has field names createdAt, updatedAt, and objectId thanks to Parse. Ideally, I would like to use createdAt with the order. Any suggestions?

Share Improve this question asked May 14, 2013 at 23:55 andy4thehuynhandy4thehuynh 2,1723 gold badges27 silver badges39 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

Possible solutions:

  1. Sort it on the server side

  2. Sort it manually:

    data['results'].sort(function(a, b) {
        return a.createdAt < b.createdAt ? -1 : 1;
    });
    

The data argument in your $.ajax request gets sent to the server; so you would use that on the server to sort your results.

If you're not sorting on the server, you need to sort in your success: callback. Suggest you use something like this:

Obj.retrieve = function(sortBy, callBack) {
    $.ajax({
        url : 'https://api.parse./1/classes/messages',
        type : 'GET',
        dataType: 'JSON',
        contentType : 'application/json',
        data : JSON.stringify({   
            text : 'value: ' // maybe not needed?
        }),
        error : function(data) {
            console.log('error: ' + data);
        },
        success: function (data) {
            data['results'].sort(function (a, b) {
                return a.sortBy - b.sortBy;
            });
            $.each(data['results'], function (n, v) {
                callBack(v.text);
            });
        });
};

Warning: This was piled in my head.

Your usage would be something like o.retrieve('createdAt', function () { });, or o.retrieve('updatedAt', function () { });, or o.retrieve('objectId', function () { });

You can just use the order method as your data type and use -.

 Obj.retrieve = function(callBack) {
   $.ajax({
       url : 'https://api.parse./1/classes/messages',
       type : 'GET',
       dataType: 'JSON',
       contentType : 'application/json',
       data : 'order=-createdAt', //REQUEST FROM NEWEST
       }),
       error : function(data) {
           console.log('error: ' + data);
       },
       success : function(data) {
          for (var i = 0; i < data["results"].length; i++)
             callBack(data["results"][i].text);
       }
   });
}; 

From http://api.jquery./jquery.getjson/ we read:

  • Data that is sent to the server is appended to the URL as a query string. If the value of the data parameter is a plain object, it is converted to a string and url-encoded before it is appended to the URL.

So for this, we should be able to rework the request URL to be: https://api.parse./1/classes/chats?order=createdAt

本文标签: javascriptHow do I implement an 39order39 property with AJAX dataStack Overflow