javascript - ajax请求返回200,json中文乱码怎么破?

查看:141
本文介绍了javascript - ajax请求返回200,json中文乱码怎么破?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

json文件

[{
    id: 2,
    name: '权限控制',
    icon: 'icon-shield',
    type: '0',
    url: '',
    layer: '0',
    pid: 0
}, {
    id: 3,
    name: '用户管理',
    icon: 'icon-shield',
    type: '1',
    url: './snippet/auth/user.html',
    layer: '0,2',
    pid: 2
}, {
    id: 4,
    name: '&测试用例&',
    icon: 'icon-shield',
    type: '1',
    url: './snippet/auth/role.html',
    layer: '0,2',
    pid: 2
}, {
    id: 5,
    name: '权限管理',
    icon: 'icon-shield',
    type: '1',
    url: 'snippet/auth/user.html',
    layer: '0,2',
    pid: 2
}, {
    id: 6,
    name: '品牌供应商管理',
    icon: 'icon-shield',
    type: '1',
    url: 'snippet/auth/user.html',
    layer: '0,2',
    pid: 2
}]

请求的完整代码:
   function menuclick(name, describe) {
    //$('div.page-title h1').empty();
    $('div.page-title h1').html(name + ' <small>' + describe + '</small>');
}
jQuery(document).ready(function() {

    $.ajax({
        type: "get",
        async: false,
        dataType: "json",
        url: "json/leftTree.json",
        beforeSend: function(data) {

        },
        success: function(data) {
            var tree = toTree(data, 0);
            //console.log(tree);
            var str, node, child = [];
            for (var o in tree) {
                (function(node, dom) {
                    if (node.children.length != 0) {
                        str = $('<li><a href="javascript:;"><i class="' + node.icon + '"></i><span class="title">' + node.name + '</span><span class="arrow"></span></a><ul class="sub-menu"></ul></li>');
                        dom.append(str);
                        for (var i in node.children) {
                            arguments.callee(node.children[i], str.find('ul.sub-menu'));
                        }
                    } else {
                        dom.append('<li><a class="ajaxify" href="' + node.url + '" 

onclick="menuclick(\'' + node.name + '\',\'' + node.icon + '\')">' + node.name + '</a></li>');
                        }
                    })(tree[o], $('ul.page-sidebar-menu'));
                }
                Metronic.init(); // init metronic core componets
                Layout.init(); // init layout
                Demo.init(); // init demo features
                QuickSidebar.init(); // init quick sidebar
                Index.init(); // init index page
                Tasks.initDashboardWidget(); // init tash dashboard widget

            },
            error: function(data) {
                console.log("error");
            }
        })
    });

↓↓控制台错误截图↓↓

解决方案

在服务端设置一下charset编码,

header("Content-type:text/html;charset=utf-8");

这篇关于javascript - ajax请求返回200,json中文乱码怎么破?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆