溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》
  • 首頁 > 
  • 教程 > 
  • 開發技術 > 
  • 決SpringMVC、tomcat、Intellij idea、ajax中文亂碼怎么解決

決SpringMVC、tomcat、Intellij idea、ajax中文亂碼怎么解決

發布時間:2020-10-30 14:51:11 來源:億速云 閱讀:302 作者:Leah 欄目:開發技術

這期內容當中小編將會給大家帶來有關決SpringMVC、tomcat、Intellij idea、ajax中文亂碼怎么解決,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

ajax 亂碼

解決方法:在contentType中添加”charset=utf-8”

$.ajax({
  url:"/rest/get",
  type:"POST",
  contentType:"application/json;charset=utf-8", //添加編碼格式
  data:JSON.stringify(a),
  dataType:"json",
  success:function(data){
    console.log("success!");
    console.log(data);
  },
  error: function (XMLHttpRequest, textStatus, errorThrown) {
    console.log(XMLHttpRequest.status);
    console.log(XMLHttpRequest.readyState);
    console.log(textStatus);
    console.log(errorThrown);
  }
});

SpringMVC亂碼

解決方法:在@RequestMapping中添加以下代碼:

接收參數編碼設置:

comsumes="application/json;charset=UTF-8"

返回參數設置:

produces="application/json;charset=UTF-8"

完整代碼如下:

@RequestMapping(value = "/post", method = RequestMethod.POST,consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
@ResponseBody
public String getDailyLog(@RequestBody String message){

  System.out.println("編碼格式為: "+System.getProperty("file.encoding"));
  System.out.println("message: "+message);
  logger.info(dailyLogShowService.getContent());
  System.out.println("System.out + "+"中文:"+dailyLogShowService.getContent());
  DailyLog dailyLog = dailyLogShowService.getDailyLog();
  logger.info(dailyLog);
  return "{\"a\":\"返回中文\"}";
}

tomcat亂碼

解決方法:進入idea->Run/Debug Configurations的tomcat->server設置,

在VM options添加 “-Dfile.encoding=utf-8”

決SpringMVC、tomcat、Intellij idea、ajax中文亂碼怎么解決

設置tomcat編碼格式

idea編輯器亂碼

如果不是上述問題,那么中文亂碼可能是由編輯器引起的。

進入idea菜單 File->Setting-> File Encoding,將3標記的三處編碼改為”UTF-8”

決SpringMVC、tomcat、Intellij idea、ajax中文亂碼怎么解決

設置idea編碼格式

然后進入idea的安裝目錄D:\setup\IntelliJ IDEA 14.0.3\bin

idea.exe.vmoptions和idea64.exe.vmoptions兩個文件的末尾追加:'-Dfile.encoding=UTF-8'

決SpringMVC、tomcat、Intellij idea、ajax中文亂碼怎么解決

可以使用Notepad++打開

決SpringMVC、tomcat、Intellij idea、ajax中文亂碼怎么解決

一個小工具

利用以下代碼輸出當前的編碼格式,如果輸出的是GBK,則需要執行上面五個步驟,直到編碼輸出為UTF-8

System.out.println(System.getProperty("file.encoding"));

小結

解決亂碼問題的五大步驟(無先后順序):

1. ajax前端設置application/json;charset=utf-8;

2. springmvc后臺添加 consumes = “application/json;charset=utf-8”, produces = “application/json;charset=utf-8”;

3. 在tomcat->server->vmoption中加入-Dfile.encoding=utf-8;

4. 在idea\bin中idea.exe.vmoptions、idea64.exe.vmoptions添加 -Dfile.encoding=utf-8;

5. 在idea的設置中搜索“File Encoding”,把三個地方的編碼全部設置為uft-8

上述就是小編為大家分享的決SpringMVC、tomcat、Intellij idea、ajax中文亂碼怎么解決了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女