【问题标题】:cant use ajax to render img tag无法使用 ajax 渲染 img 标签
【发布时间】:2016-12-05 16:34:34
【问题描述】:

我使用 ajax 来返回当前用户个人资料图像,但它为这个 img 带来了真正的 src,但如果我在我的 html 工作中使用这个 img 标签,它不会显示图像的位置在我的布局中,但它不会从请求中工作在mvc5中

 public async Task<string> GetImageUrl()
    {
        var id = User.Identity.Name.SplitFormAuthentication().id;
        var firstOrDefault =await _user.Where(m => m.Id == id).FirstOrDefaultAsync();
        if (firstOrDefault != null)
        {
            string url =string.Format("<img src='~/File/Profile/{0}' id='' alt='' />", firstOrDefault.Avatar);
           var s=WebUtility.HtmlDecode(url);
            return s;
        }
        return "";
    }

在布局ajax调用是

<script>
        $(document).ready(function () {
            GetImageUrl();
            function GetImageUrl() {

                //alert($(this).parent().parent().parent().attr("class"));

                var pa = $(".addEdu");

                $.ajax({
                    url: "@Url.Action("GetImageUrl","Home")",
                    type: "POST",
                    dataType: "html",
                    context: this,
                    data: {

                    },
                    success: function (data) {
                        alert(data);
                        $("#profileimg").html(data);
                        $(".ssss").html(data);

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("");
                    }
                });

            }
        })
    </script>

【问题讨论】:

    标签: javascript jquery html ajax asp.net-mvc


    【解决方案1】:

    无法像这样解析图片的url。

    string url = string.Format("<img src='~/File/Profile/{0}' id='' alt='' />", firstOrDefault.Avatar);
    

    试试

    string url = string.Format("<img src='{0}' id='' alt='' />", Url.Content(string.Format("~/File/Profile/{0}", firstOrDefault.Avatar)));
    

    【讨论】:

      猜你喜欢
      • 2019-12-31
      • 2012-02-15
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多