I have a question on how to filter results from a API
, and would like to know if anyone has had this problem before.
Let's say that we have to access a API
that is on a server other than ours and returns a response in JSON
with a structure similar to the following
[
{
"nome": "Joshua",
"sex": "M"
},
{
"name": "Marie",
"sex": "F"
},
{
"name": "Frank",
"sex": "M"
}
]
Of course this is just an example, because the answer could be millions of results. The callback
parameter to start communication JSONP
is ?callback=...?
.
What I would like to know is if there is a way to filter these results, suppose we only want to return people from sexo
male ( M
), without having to do client-side filtering. I remember that to make this call, it can only be done through Javascript without resorting to another type of language.
My first idea would be to make a call using jQuery this way:
$.ajax({
type: 'GET',
url: "https://url-to-api?callback=?",
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
data:{'sex':'M'},
dataType: 'jsonp',
success: function(json) {
console.dir(json);
},
error: function(e) {
console.log(e.message);
}
Does anyone know a way to do a filtering while doing the request? Thanks in advance for your availability.