我無法弄清楚我的代碼出了什麼問題,以及爲什麼我的API/AJAX調用不會顯示我要調用的圖像。它出現在我console.log(結果)10個圖像被調用,但他們不會顯示。這裏是我的jQuery代碼.. //當窗口加載...功能會發生 的window.onload =函數(){jquery API請求錯誤/不會顯示giphy的
var musiciansList = [];
var inputBox = $('#submitButton')
//whatever musician the user submits will appear
$('#submitButton').on('click', function(){
var input=$('#submitButton');
var userInput=inputBox.val();
musiciansList.push(userInput);
renderButtons();
});
function renderButtons() {
var button = $('<button>');
button.text(musiciansList[musiciansList.length-1]);
button.addClass('band')
$('.container').append(button);
};
//When I click this button a function will happen
$(document).on('click', '.band', function() {
//variable queryUrl for giphy
var queryUrl = "http://api.giphy.com/v1/gifs/search?q=music&api_key=dc6zaTOxFJmzC&limit=10";
//requesting information giphy
$.ajax({
url: queryUrl,
method: 'GET'
})//recieving information from giphy
.done(function(response) {
//returns the response from the website
var results = response.data;
var imageUrl = response.data.image_original_url;
var musicians = $('<img>');
console.log(queryUrl)
//takes var musicians and adds attr src and imageUrl
musicians.attr('src', imageUrl);
musicians.attr('alt', 'musician');
$("#images").push(imageUrl)
//prepend puts the images in the beginning
$("#images").prepend(imageUrl);
$('<img>').val();
console.log(results)
console.log(imageUrl)
//empty gifs button
$('#clearButton').click(function(event){
$(musicians).remove()
});
});
});
};
'response.data'不會出現有一個屬性'image_original_url'? – guest271314
您錯過了將/音樂家添加到$(「#images」)/任何DOM元素 – Hemakumar