【问题标题】:How decode html content bound in json?如何解码 json 中绑定的 html 内容?
【发布时间】:2017-04-18 21:48:45
【问题描述】:

我正在使用asp.net 应用程序并使用ajax 调用,下面是我的代码。下面是我的网络方法,它工作正常并给出ajax 调用的响应。

ADController adc = new ADController();
        DataTable dt = adc.GetGeneral(Convert.ToInt32( AnnouncementId));// GetAnnouncementsByIDAndRead(Convert.ToInt32(AnnouncementId), Convert.ToInt32(userid));
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
        Dictionary<string, object> childRow;
        foreach (DataRow row in dt.Rows)
        {
            childRow = new Dictionary<string, object>();
            foreach (DataColumn col in dt.Columns)
            {
                if (col.ColumnName == "description")
                {
                    childRow.Add(col.ColumnName, HttpUtility.HtmlDecode( Convert.ToString( row[col]) )as object);
                }
                else 
                childRow.Add(col.ColumnName, row[col]);
            }
            parentRow.Add(childRow);
        }
        return jsSerializer.Serialize(parentRow);

以下是我的 ajax 代码,它工作正常并且在调用时提供数据也很好。

 function fnshowAncDetails(AnnouncementId, userid) {
    $(".loading").show();
    var url = $("[id$='hdURLt']").val();
    $("[id$='btnSaveMD']").show();
    $.ajax({
        type: "POST",
        url: url + "/GetInfo.aspx/General",
        data: '{AnnouncementId:"' + AnnouncementId + '",userid:"' + userid + '"}',
        contentType: "application/json; charset=utf-8",
        //dataType: "json",
        success: OnSuccessSetCGiven,
        error: function (response) {

        }
    });
    var vtext = $("[id$='lblAnnoucement']").text();
    if (vtext != 0) {
        vtext = vtext - 1;
    }
    $("[id$='lblAnnoucement']").text(vtext);
}

下面是我的成功方法

  function OnSuccessSetCGiven(response) {
    var parsed = $.parseJSON(response.d);

    $("[id$='htititlen']").text(parsed[0].Title);
    $("[id$='divNotifBody']").text(parsed[0].Description);


    $("[id$='divadded']").text("By:"+parsed[0].FirstName + " " + parsed[0].LastName);
    $("#divNotifdetails").modal('show');
    $(".modal-backdrop").css('z-index', '0');
    $(".loading").hide();

    var formattedTime = parsed[0].stime.Hours + ":" + parsed[0].stime.Minutes;
    //$("[id$='divtime']").text(formattedTime);
    $("[id$='divdate']").text("Time:" +parsed[0].startdate + " " + formattedTime);
}

现在我的问题是在描述中可能有 html 标签,表示格式化的 html,如 &lt;p&gt;xxx&lt;/p&gt;&lt;b&gt;sdf&lt;/b&gt;。所以它没有加载为粗体, 如何呈现格式化的 html?

【问题讨论】:

    标签: javascript c# html json ajax


    【解决方案1】:

    使用 jQuery .html 函数而不是 .text

    function OnSuccessSetCGiven(response) {
        ...
        $("[id$='divNotifBody']").html(parsed[0].Description);
        ...
    }
    

    但请注意,您会有JS注入漏洞,因此您必须清理描述字段中的HTML代码并删除不需要的属性和标签(例如:&lt;script&gt;&lt;any onclick=""&gt;等)

    更新:

    顺便说一下,我不熟悉这种选择语法:

    $("[id$='divNotifBody']")
    

    假设你想选择一个id为“divNotifBody”的div,为什么不直接使用:

    $("#divNotifBody")
    

    【讨论】:

    • 谢谢回复。但上面不工作,还是一样。只是我试过了。你能有其他解决方案吗?
    • 如果描述包含有效的 html 字符串,它没有理由不起作用。请参阅 jquery 中的 .html 文档:api.jquery.com/html/#html-htmlString
    • @RonDadon $("[id$='divNotifBody']") 是一个有效的 'ends with' CSS3 选择器,请参阅w3.org/TR/css3-selectors/#attribute-substrings,如果你有例如它很有用。在这个例子中firstdivNotifBodyseconddivNotifBody
    • @Tyblitz 每天都在学习新事物 :) 谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2012-06-21
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多