jquery AJAX 空数据返回html时更改为其他值

flvlnr44  于 2023-04-05  发布在  jQuery
关注(0)|答案(2)|浏览(138)

这是我的 AJAX 代码,我想用它来从API中获取数据,以显示在div中。当我查询数据时,我得到的value.application_iconnull,所以这个文件是空白的
value.application_iconnull时,我想使用value.application_image。我不知道如何配置。

$.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>'
          );
        })
      },
svujldwt

svujldwt1#

$.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);
    }
});
2vuwiymt

2vuwiymt2#

试试这个代码片段。

$.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);
    }
});

相关问题