weixin_33701251 2015-08-14 15:12 采纳率: 0%
浏览 100

JavaScript utf8编码?

I have the following ajax call. It can send the request and get the response as expected. But I can't seem to get it correctly displayed in li items.

$.ajax({
    url:  "{% url 'users:profile_page_tags_get' 'primary' %}",
    type: 'GET',
    dataType: "json",
    success: function(data) {
        for ( var an_option in data.pd_options ){
            $("#selectable").append( "<li class='ui-widget-content-item ui-selectee'>"+an_option[0]+"</li>" );
        }
    }
})

It does get displayed when

$(".try1").html(data.pd_options[2][1]);

The response is a JSON array. The characters are encoded in utf8 I believe.

{"pd_options": [[2, "\u5316\u5b66\u5de5\u7a0b"], [1, "\u5316\u5de5"], [3, "\u571f\u6728\u5de5\u7a0b"]]}
  • 写回答

1条回答 默认 最新

  • weixin_33749131 2015-08-14 15:16
    关注

    You misunderstood for...in loop. In your case, an_option is not element of array, but an index, so you should try data.pd_options[an_option] to get this element.

    Your code should be:

    $.ajax({
        url:  "{% url 'users:profile_page_tags_get' 'primary' %}",
        type: 'GET',
        dataType: "json",
        success: function(data) {
            for ( var an_option in data.pd_options ){
                $("#selectable").append( "<li class='ui-widget-content-item ui-selectee'>"+data.pd_options[an_option][1]+"</li>" );
            }
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备