AJax Null Data 当返回到 HTML 时更改为另一个值

huangapple go评论64阅读模式
英文:

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: &quot;GET&quot;,
      contentType: &quot;application/json&quot;,
      url: endpoint + apiKey,
      success: function(response) {
        $.each(response, function(key, value) {
          $(&#39;.first-set&#39;).append(
            &#39;&lt;li&gt;&lt;a href=&quot;&#39; + value.application_base_url + &#39;&#39; + value.application_url + &#39;&quot;&gt; \n &#39; +
            &#39;&lt;div&gt; \n &#39; +
            &#39;&lt;i class=&quot;&#39; + value.application_icon + &#39;&quot;&gt;&lt;/i&gt; \n &#39; +
            &#39;&lt;/div&gt;&#39; +
            &#39;&lt;span&gt;&#39; + &quot;Search&quot; + &#39;&lt;/span&gt;&#39; +
            &#39;&lt;/a&gt;&lt;/li&gt;&#39;
          );
        })
      },

答案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: &quot;GET&quot;,
        contentType: &quot;application/json&quot;,
        url: endpoint + apiKey,
        success: function(response) {
            $.each(response, function(key, value) {
                var icon = !value.application_icon?  value.application_image: value.application_icon;
                $(&#39;.first-set&#39;).append(
                        &#39;&lt;li&gt;&lt;a href=&quot;&#39; + value.application_base_url + value.application_url + &#39;&quot;&gt; \n &#39; +
                        &#39;&lt;div&gt; \n &#39; +
                        &#39;&lt;i class=&quot;&#39; + icon + &#39;&quot;&gt;&lt;/i&gt; \n &#39; +
                        &#39;&lt;/div&gt;&#39;+
                        &#39;&lt;span&gt;Search&lt;/span&gt;&#39; +
                        &#39;&lt;/a&gt;&lt;/li&gt;&#39;
                    );
            });
        },
        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: &quot;GET&quot;,
        contentType: &quot;application/json&quot;,
        url: endpoint + apiKey,
        success: function(response) {
            $.each(response, function(key, value) {
                var icon = value.application_icon;
                if (!icon) {
                    icon = value.application_image;
                }
                if (icon) {
                    $(&#39;.first-set&#39;).append(
                        &#39;&lt;li&gt;&lt;a href=&quot;&#39; + value.application_base_url + value.application_url + &#39;&quot;&gt; \n &#39; +
                        &#39;&lt;div&gt; \n &#39; +
                        &#39;&lt;i class=&quot;&#39; + icon + &#39;&quot;&gt;&lt;/i&gt; \n &#39; +
                        &#39;&lt;/div&gt;&#39;+
                        &#39;&lt;span&gt;Search&lt;/span&gt;&#39; +
                        &#39;&lt;/a&gt;&lt;/li&gt;&#39;
                    );
                }
            });
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });
    

    Or else use like this with for loop

    $.ajax({    
            type: &quot;GET&quot;,
            contentType: &quot;application/json&quot;,
            url: endpoint + apiKey,
            success: function(response) {
                for(var i=0; i&lt;response.length; i++) {
                    var value = response[i];
                    var icon = value.application_icon;
                    if (!icon) {
                        icon = value.application_image;
                    }
                    if (icon) {
                        $(&#39;.first-set&#39;).append(
                            &#39;&lt;li&gt;&lt;a href=&quot;&#39; + value.application_base_url + value.application_url + &#39;&quot;&gt; \n &#39; +
                            &#39;&lt;div&gt; \n &#39; +
                            &#39;&lt;i class=&quot;&#39; + icon + &#39;&quot;&gt;&lt;/i&gt; \n &#39; +
                            &#39;&lt;/div&gt;&#39;+
                            &#39;&lt;span&gt;Search&lt;/span&gt;&#39; +
                            &#39;&lt;/a&gt;&lt;/li&gt;&#39;
                        );
                    }
                });
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        });
    

    hope it will help you.

    huangapple
    • 本文由 发表于 2023年3月31日 17:51:42
    • 转载请务必保留本文链接:https://go.coder-hub.com/75897112.html
    匿名

    发表评论

    匿名网友

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定