简体   繁体   中英

How do I pass parameters into a gallery search using the Imgur API

I'm just learning how to do Ajax calls using jQuery and I'm attempting to build a simple image search using the Imgur API. I've gotten the basic search to return an image object, but I cannot figure out how to pass the advanced search parameters to the search.

Here is my code:

$('#findImage').on('click', function(){

    var searchTerm = $('#image-input').val();

    var queryURL = "https://api.imgur.com/3/gallery/search/q?=" + searchTerm;

    $.ajax({
        url: queryURL,
        method: 'GET',
        headers: {
       Authorization: 'Client-ID ' + clientID
         }
    })

     .done(function(response) {

    for (var i = 0; i < 10; i++) {

        var results = response.data;

        console.log(results[i]);

        }

     });

So that returns an object, but what I'm hoping to do is only search for jpegs. In the Imgur documentation it says the following:

Simple Search Query Parameters Key Value q Query string (note: if advanced search parameters are set, this query string is ignored). This parameter also supports boolean operators (AND, OR, NOT) and indices (tag: user: title: ext: subreddit: album: meme:). An example compound query would be 'title: cats AND dogs ext: gif'

Advanced Search Query Parameters

Key Value

q_all Search for all of these words (and)

q_any Search for any of these words (or)

q_exactly Search for exactly this word or phrase

q_not Exclude results matching this

q_type Show results for any file type, jpg | png | gif | anigif (animated gif) | album

q_size_px Size ranges, small (500 pixels square or less) | med (500 to 2,000 pixels square) | big (2,000 to 5,000 pixels square) | lrg (5,000 to 10,000 pixels square) | huge (10,000 square pixels and above)

My question is, how do I pass these advanced search parameters to the ajax call?

您可以像这样设置参数值:

queryURL = "https://api.imgur.com/3/gallery/search/q?=" + search term + "&q_type=jpeg&q_size_px=500";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM