JSON API Callback

3

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.

    
asked by anonymous 18.10.2015 / 05:44

2 answers

1

If the site where you are going to fetch JSON does not allow filtering parameters then you have to do this on the client side.

Take a look at the API for this site.

By doing the filtering on the client side you can use .filter() . Check if the JSON that the site has passed is processed as JSON or already as array, I will include this check in the example below:

var parametroFiltragem = 'M';
$.ajax({
    type: 'GET',
    url: "https://url-to-api?callback=?",
    jsonpCallback: 'jsonCallback',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function (json) {
        if (typeof json == 'string') json = JSON.parse(json);
        var filtrados = json.filter(function(obj){
            return obj.sex == parametroFiltragem;
        });
        // fazer algo somente com os "filtrados"
    },
    error: function (e) {
        console.log(e.message);
    }
});
    
18.10.2015 / 09:06
0

I think the code below might be what you're looking for:

$.ajax({
    type: 'GET',
    url: "https://url-to-api/pagina",
    data: {
        sex: "M"
    },
    dataType: "jsonp",
    crossDomain: true,
    cache: false,
    success: function (json) {
        alert(json);
    },
    error: function (e) {
        alert(e.statusText +": "+ e.status);
    }
});

In your comment you said that the request will be made to another domain, so we activated crossDomain, which allows you to perform ajax request for other domains.

In this case, by default, you have disabled the cache, but it is up to you to use it or not.

In the date parameter I just removed the quotation marks from the word "sex".

  

Note: "Your sex filter will only work if the API that is on the other   you have implemented this parameter as a filter, otherwise you   you will have to return all the data and create the client-side filter. "

    
18.10.2015 / 06:24