How to solve Chinese garbled code when jQuery Ajax returns JSON

Time:2021-1-25

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 ('failed to get data! ');  
        },  
        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 ('failed to get data! '); 
        }, 
        success: function(json){ 
          jsObject = eval(json); 
        } 
    }); 
    return jsObject;</span> 
action:

Java code

JSONArray json =  JSONArray.fromObject (syslist); // syslist is a list  
//Setting contenttype of response to solve Chinese garbled code  
    response.setContentType("text/html;charset=UTF-8");  
    response.getWriter().print(json.toString());  
    return null; 
Java code 
<span style="font-size: x-small;">JSONArray json =  JSONArray.fromObject (syslist); // syslist is a list 
//Setting contenttype of response to solve Chinese garbled code 
    response.setContentType("text/html;charset=UTF-8"); 
    response.getWriter().print(json.toString()); 
    return null;</span>

The code is so simple that it doesn’t explain. If you have any questions, please leave me a message and I will get in touch with you at the first time.