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 badges4 Answers
Reset to default 1Possible solutions:
Sort it on the server side
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
版权声明:本文标题:javascript - How do I implement an 'order' property with AJAX data? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745172409a2646056.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论