JSON API Callback

Asked

Viewed 1,059 times

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].

  • I’m sorry, I didn’t even realize it.

  • Quiet guy.

  • By the way, if you know how to fix my problem,... ;)

  • I’m trying to understand, I just started with javascript. The way you tried it didn’t work?

  • 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)

Show 1 more comment

2 answers

1

If the JSON site does not allow filtering parameters then you have to do it on the client side.

Take a look at the API of this site.

Filtering on the client side you can use the .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);
    }
});
  • Your solution works. And that’s what I had initially thought. The point is that there are up to 6 search parameters here. And research can even be complex.For example, it is necessary to filter by price (higher, lower, between) and other types of combination. Is it a good performance solution? The only information I have from the API is the following: The tool itself should allow for the user to perform simple Filtering based on the criteria Listed.The Filtering options should be appropriate for its type (numerical etc.). The callback Parameter to initiate JSONP Communication is "?callback="

  • @Ma1990 by this description in English say that you can do filtering. You can show the link to the documentation?

  • I don’t have any documentation. The only one I have is the API URL.

  • @Ma1990 and what is the url?

  • @Ma1990, I’ve been looking around the Indian site and I can’t find anything that indicates parameters to filter. How did you find this API? you have nowhere to ask?

  • @Ma1990 can you share this documentation? see if it says anything about filters...

  • What’s more, I shared it above. Then there’s the following: The callback Parameter to initiate JSONP Communication is "?callback=...". Invalid or Missing callbacks will invoke a 400 Bad Request The user should be Able to filter Against These criterias • Series number • Has Physx • Has SLI • Is In Stock • Is Not A Preorder • Price Is Below, Above, or Between

  • @Ma1990 and received information that this can be done in the url? or is it to make a filter on the client? Doing it on the client makes sense to me, otherwise you’ll have to guess and it seems strange to me that you were given a task to "guess" what the API does.

  • It’s to filter the client What doesn’t make sense to me is to request everything and then implement a filter. Even more the filter can be quite complex, taking into account that it is to filter through immense parameters. "We require a javascript-based widget that can be Dropped onto a NVIDIA owned Property. The widget should pull a JSON feed from a determined Location. The JSON feed contains product information, and some purchasing data. The tool itself should allow for the user to perform simple Filtering based on the criteria Listed."

  • At a minimum, the user should be Shown the Technologies Inside the Graphics card, the Specifications, the Graphics card "image", and a link to a Detail page. The Filtering options should be appropriate for its type (numerical, Boolean, etc.) https://www.nvidia.eu/developer-test/buy.php?callback= The callback Parameter to initiate JSONP Communication is "callback=..." Invalid or Missing callbacks will invoke a 400 Bad Request The user should be Able to filter Against These criterias • Series number • Has Physx • Has SLI • Is In Stock • Is Not A Preorder • Price Is Below, Above, or Between

  • This is the documentation of the problem, nothing else. Which leads me to believe that it is to filter on the client side after making the request.

  • @Exact ma1990. I think you need to make selects in HTML populated with what JSON has based on the "Series number • Has Physx • Has SLI • Is In Stock • Is Not A Preorder • Price Is Below, Above, or Between". Do you know how to do that? If I can’t help/adapt the answer later.

  • I’d appreciate your help with that :)

  • Hello Sergio. Can you always help me with this filtering? Another question. Considering that you have to do the manipulation after Success. How can I do the content styling? I do this in response and then append to an element that already exists on the page? Is there any way to pass the object array to HTML and do the manipulation there? How is it done in Angularjs with ng-repeat.

  • @Ma1990 the code to do this is not that simple. I’ve spent a lot of time putting together an example that needs fine-tuning: http://jsfiddle.net/gfrsLx92/ I guess that’s what you’re looking for... it took a while and it’s not 100%. Know how to do the rest?

  • Thanks for this. I’m taking your idea and doing it with Angularjs. I think it removes the complexity of "building" HTML.

Show 11 more comments

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 for another domain, for this we activated crossDomain, feature that allows you to perform ajax request for other domains.

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

In the date parameter I only removed the quotation marks of the word "sex".

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.