Home > Web Front-end > JS Tutorial > body text

How to solve the problem of Chinese garbled characters when JQuery ajax returns json

亚连
Release: 2018-05-25 09:45:53
Original
2262 people have browsed it

Recently, some friends reported that Chinese garbled characters appeared when using jquery.ajax to return json. It is very difficult to encounter this problem. Let me help you solve the problem of Chinese garbled characters when JQuery ajax returns json. Interested friends can follow along to learn more. Bar

Using jQuery ajax call to return json, Chinese garbled 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;
Copy after login

Js code

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

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>
Copy after login

The above is I compiled it for everyone, I hope it will be helpful to everyone in the future.

Related articles:

How to use the ajax post method in the Django framework

Using jquery ajax in django post data appears 403 Error solution

Detailed explanation of ajax jtemplate to implement dynamic paging

The above is the detailed content of How to solve the problem of Chinese garbled characters when JQuery ajax returns json. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!