Home >Web Front-end >JS Tutorial >How to deal with the Chinese garbled json returned by jquery+ajax

How to deal with the Chinese garbled json returned by jquery+ajax

php中世界最好的语言
php中世界最好的语言Original
2018-04-26 14:53:012711browse

This time I will show you how to deal with jquery ajax returning json Chinese garbled characters, and what are the precautions when dealing with jquery ajax returning json Chinese garbled characters. The following is a practical case, let's take a look.

Using jQuery ajax call to return json, Chinese Garbled code problem

Js code is as follows:

$.ajax({  
        url: '/test/testAction.do?method=test',  
        type: 'POST',  
        dataType: 'json',  
        timeout: 5000,  
        async: false,  
        error: function(){  
          alert('获取数据失败!');  
        },  
        success: function(json){  
          jsObject = eval(json);  
        }  
    });  
    return jsObject;

Js code

<span style="font-size: x-small;">$.ajax({ 
        url: '/test/testAction.do?method=test', 
        type: 'POST', 
        dataType: 'json', 
        timeout: 5000, 
        async: false, 
        error: function(){ 
          alert('获取数据失败!'); 
        }, 
        success: function(json){ 
          jsObject = eval(json); 
        } 
    }); 
    return jsObject;</span> 
action:

Java Code

JSONArray json = JSONArray.fromObject(SysList);//SysList是一个List  
//   设置response的ContentType解决中文乱码  
    response.setContentType("text/html;charset=UTF-8");  
    response.getWriter().print(json.toString());  
    return null; 
Java代码 
<span style="font-size: x-small;">JSONArray json = JSONArray.fromObject(SysList);//SysList是一个List 
//   设置response的ContentType解决中文乱码 
    response.setContentType("text/html;charset=UTF-8"); 
    response.getWriter().print(json.toString()); 
    return null;</span>

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

JSONP solves ajax cross-domain problems (with code)

What are the implementations of AJAX secondary linkage method

The above is the detailed content of How to deal with the Chinese garbled json returned by jquery+ajax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn