3
I have a question as to how to filter results from a API
, and I wonder if anyone has had this problem before.
Let’s say we have to access a API
which is on a server other than ours and which returns an answer 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 one example, because the answer could be millions of results. The callback
to initiate the communication JSONP
is ?callback=...?
.
What I’d like to know is if there’s a way to filter these results, let’s imagine that we just want to return people from sexo
masculine (M
), without having to do a customer-side filtering. Remember that to make this call, 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 filter when making the request?
Thanks for your time.
This is the [en.so], translate your question or post on [so].
– Jéf Bueno
I’m sorry, I didn’t even realize it.
– Nocktu
Quiet guy.
– Jéf Bueno
By the way, if you know how to fix my problem,... ;)
– Nocktu
I’m trying to understand, I just started with javascript. The way you tried it didn’t work?
– Jéf Bueno
The way I did, just return the whole list. What I wanted was to filter this list right in the request. , pass parameters. But since I don’t have control in the api I don’t know how to pass these parameters. I didn’t want to loop through all the Bjects and see which one is male (M)
– Nocktu