我的Javascript页面没有从giphy API获取数据

时间:2017-11-10 06:20:35

标签: javascript jquery api giphy-api

我已将Giphy API链接到我的应用中,似乎无法弄清楚为什么呼叫无效。我想也许这是我的钥匙,但我插入了一个公钥,但仍然没有用。我是否错误地调用了API?我确实检查了giphy API的文档,看起来我正在使用正确的参数。

这是我的代码:

var feelings = ["Angry", "Happy", "Excited", "Confused"];
var currentGif;
var pausedGif;
var animatedGif;
var stillGif;



function createButtons() {

    $('#buttons').empty();

    for(var i=0; i < feelings.length; i++) {

        var showBtn = $('<button>').text(feelings[i]).addClass('showBtn').attr({'data-name' : 
feelings[i]});

        $('#buttons').append(showBtn);
    }


    $('showBtn').on('click', function() {

        $('display').empty();

        var currentFeeling = $(this).data('name');

        var giphyURL = "http://api.giphy.com/v1/gifs/search?q=" + 
currentFeeling + "&api_key=xxx&limit=8";

        $.ajax({

        url: giphyURL,
        method: 'GET'
    }).done(function(giphy) {

        currentGif = giphy.data;

        $.each(currentGif, function(index, value) {

            animatedGif = value.images.original.url;
            pausedGif = value.images.original_still.url;

            var thisRating = value.rating;

            if(thisRating == '') {

                thisRating = 'unrated';
            }

            var rating = $('<h3>').html('Rated: '+ thisRating).addClass('ratingStyle');

            stillGif = $('<img>').attr('data-animated', animatedGif).attr('data-paused', pausedGif).attr('src', pausedGif).addClass('playOnHover');

            var fullGifDisplay = $('<button>').append(rating, stillGif);

            $('#display').append(fullGifDisplay);
        });



    });

    });
}

$(document).on('mouseover', '.playOnHover', function() {

    $(this).attr('src', $(this).data('animated'));
});

$(document).on('mouseleave', '.playOnHover', function() {

    $(this).attr('src', $(this).data('paused'));
});

$('#add-feeling').on('click', function() {

    var newFeeling = $('#feeling-input').val().trim();

    feelings.push(newFeeling);

    createButtons();

    return false;
});

createButtons();

2 个答案:

答案 0 :(得分:0)

嘿,我已经修改了这段代码, 你使用https intsted of http -

var giphyURL = "https://api.giphy.com/v1/gifs/search?q=" + 
currentFeeling + "&api_key=0BScOL3fj1asLChkeb0QOnvJcNr7VbHc&limit=8";

分享您的搜索参数 //api.giphy.com/v1/gifs/search?q=parameter

答案 1 :(得分:0)

我想通了......我错过了课程和id(#,.)..叹息