英文:
AJax Null Data Change to another valeu when return to html
问题
以下是已经翻译好的代码部分:
$.ajax({
type: "GET",
contentType: "application/json",
url: endpoint + apiKey,
success: function(response) {
$.each(response, function(key, value) {
$('.first-set').append(
'<li><a href="' + value.application_base_url + value.application_url + '">' +
'<div> ' +
'<i class="' + value.application_icon + '"></i> ' +
'</div>' +
'<span>Search</span>' +
'</a></li>'
);
})
},
});
英文:
This is my AJAX code that I want to use to get data from an API to show in a div
. When I query the data I get value.application_icon
is null
so this file is blank
I want to use value.application_image
when value.application_icon
is null
. I don't know how to config this.
<!-- language: lang-js -->
$.ajax({
type: "GET",
contentType: "application/json",
url: endpoint + apiKey,
success: function(response) {
$.each(response, function(key, value) {
$('.first-set').append(
'<li><a href="' + value.application_base_url + '' + value.application_url + '"> \n ' +
'<div> \n ' +
'<i class="' + value.application_icon + '"></i> \n ' +
'</div>' +
'<span>' + "Search" + '</span>' +
'</a></li>'
);
})
},
答案1
得分: 2
$.ajax({
type: "GET",
contentType: "application/json",
url: endpoint + apiKey,
success: function(response) {
$.each(response, function(key, value) {
var icon = !value.application_icon ? value.application_image : value.application_icon;
$('.first-set').append(
'
'
'' +
'
' +
'Search' +
'
'
);
});
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
英文:
$.ajax({
type: "GET",
contentType: "application/json",
url: endpoint + apiKey,
success: function(response) {
$.each(response, function(key, value) {
var icon = !value.application_icon? value.application_image: value.application_icon;
$('.first-set').append(
'<li><a href="' + value.application_base_url + value.application_url + '"> \n ' +
'<div> \n ' +
'<i class="' + icon + '"></i> \n ' +
'</div>'+
'<span>Search</span>' +
'</a></li>'
);
});
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
答案2
得分: 1
尝试这段代码片段。
$.ajax({
type: "GET",
contentType: "application/json",
url: endpoint + apiKey,
success: function(response) {
$.each(response, function(key, value) {
var icon = value.application_icon;
if (!icon) {
icon = value.application_image;
}
if (icon) {
$('.first-set').append(
'<li><a href="' + value.application_base_url + value.application_url + '">' +
'<div>' +
'<i class="' + icon + '"></i>' +
'</div>'+
'<span>Search</span>' +
'</a></li>'
);
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
或者像这样使用for循环。
$.ajax({
type: "GET",
contentType: "application/json",
url: endpoint + apiKey,
success: function(response) {
for(var i=0; i<response.length; i++) {
var value = response[i];
var icon = value.application_icon;
if (!icon) {
icon = value.application_image;
}
if (icon) {
$('.first-set').append(
'<li><a href="' + value.application_base_url + value.application_url + '">' +
'<div>' +
'<i class="' + icon + '"></i>' +
'</div>'+
'<span>Search</span>' +
'</a></li>'
);
}
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
希望这对你有所帮助。
英文:
Try this code snippet.
$.ajax({
type: "GET",
contentType: "application/json",
url: endpoint + apiKey,
success: function(response) {
$.each(response, function(key, value) {
var icon = value.application_icon;
if (!icon) {
icon = value.application_image;
}
if (icon) {
$('.first-set').append(
'<li><a href="' + value.application_base_url + value.application_url + '"> \n ' +
'<div> \n ' +
'<i class="' + icon + '"></i> \n ' +
'</div>'+
'<span>Search</span>' +
'</a></li>'
);
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
Or else use like this with for loop
$.ajax({
type: "GET",
contentType: "application/json",
url: endpoint + apiKey,
success: function(response) {
for(var i=0; i<response.length; i++) {
var value = response[i];
var icon = value.application_icon;
if (!icon) {
icon = value.application_image;
}
if (icon) {
$('.first-set').append(
'<li><a href="' + value.application_base_url + value.application_url + '"> \n ' +
'<div> \n ' +
'<i class="' + icon + '"></i> \n ' +
'</div>'+
'<span>Search</span>' +
'</a></li>'
);
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
hope it will help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论