[英]Can't incorporate if/else statement into JSON response
Pulling data from a API and displaying IMG's from it. 从API提取数据并从中显示IMG。
I'm trying to incorporate a if/else statement that essentially display's photos if the search term has data from the response, and if it doesn't then display text saying no results. 我正在尝试合并一个if / else语句,如果搜索项包含响应中的数据,则该if / else语句本质上显示照片,如果没有,则显示没有结果的文本。
Currently it displays the photos if there is a JSON result, but still doesn't display text/consolelog if there isn't. 当前,如果有JSON结果,它将显示照片,但如果没有,则不会显示文本/控制台。 I don't understand why it isn't hitting the else statement. 我不明白为什么它没有达到else语句。
Here is the code. 这是代码。 I can't do a codepen since it will have my API key. 我无法执行Codepen,因为它将具有我的API密钥。 Is there something obvious I'm not seeing? 有什么明显的我没看到吗? This should be so simple and easy to do, yet I can't. 这应该是如此简单和容易,但是我做不到。
// Search for Photos
$('form').submit(function (e) {
e.preventDefault();
// API Calls
let input = document.getElementById("search").value;
let $submitButton = $('#submit');
let searchPhoto = API + 'search/photos?' + client_id + '&page=1&query=' + input;
// Ajax part
$.getJSON(searchPhoto, function (response) {
// Create beginning of Bootstrap card
let photoHTML = '<div class="col-12 col-sm-12">';
if (response.length !== 0) {
// Loop over each response photo, putting it into a unique card
$.each(response.results, function (i, photo) {
// Card background
let photoBackground = photo.urls.regular;
// Download link
let download = photo.links.download + "?force=true";
// Create a link to be clicked by the download button
link = document.createElement('a');
link.href = download;
link.download = 'Download.jpg'; // The file name suggestion for the user.
document.body.appendChild(link);
// Add each card element
photoHTML += '<article class="card animated fadeInLeft text-center">';
photoHTML += '<img class="card-img-top img-responsive preview" src=' + photoBackground + '/>';
photoHTML += '<div class="card-block">';
photoHTML += '<h4 class="card-title" id="randomTitle"></h4>';
photoHTML += '<button type="button" class="btn btn-outline-primary common_class" name='+download+' id="div' + i + '">Download</button>'; // Create unique ID
photoHTML += '</article>';
})
} else {
photoHTML += '<p>Sorry dude, please work.</p>';
}
// End Card
photoHTML += '</div>';
// Put each card into a div
$('#testing').html(photoHTML);
let currently_clicked_id = '';
let myID = $(document).on('click', '.common_class', function () {
currently_clicked_id = $(this).attr('id');
return currently_clicked_id;
});
$('.common_class').click(function (e) {
// Call download link
// console.log(e.target.id);
console.log(e.target.name);
link.href = e.target.name;
link.click();
});
})
My guess would be response
is not empty. 我的猜测是response
不为空。 Since you are just looping response.results, i suggest you can use condition if(response.results !== undefined && response.results.length > 0)
由于您只是循环response.results,我建议您可以使用条件if(response.results !== undefined && response.results.length > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.