首頁  >  文章  >  web前端  >  jquery post中文亂碼怎麼辦

jquery post中文亂碼怎麼辦

coldplay.xixi
coldplay.xixi原創
2020-11-25 14:16:072190瀏覽

jquery post中文亂碼的解決方法:1、前台post請求的時候對發送的資料進行【encodeURIComponent()】編碼;2、後台用【UTF-8】轉譯。

jquery post中文亂碼怎麼辦

本教學操作環境:windows7系統、jquery3.2.1版,此方法適用於所有品牌電腦。

jquery post中文亂碼的解決方法:

前台post請求的時候對發送的資料進行encodeURIComponent()編碼

例如:

var transactType= $("#transactType").attr("value");
var content=encodeURIComponent($("#content").html());
var title=encodeURIComponent($("#title").val());
$.post(      
"${path}/transact!addTransact.action",
 {"content":content,"title":title},
 function(data){        
   if(data=='1'){           
    alert("保存成功!");           
    DG.cancel();          
  }else{           
    alert("保存失败!");        
  }
}
);

後台:

UTF-8轉譯

transactType = URLDecoder.decode(getStringParameter("transactType"),"UTF-8");
content =  URLDecoder.decode(getStringParameter("content"),"UTF-8");
title =  URLDecoder.decode(getStringParameter("title"),"UTF-8");

即可解決jQuery post請求中文亂碼問題。

相關學習推薦:js影片教學

以上是jquery post中文亂碼怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn