ajax請求中文亂碼
使用ajax將頁面中文數據傳送到後臺時,往往會出現亂碼的情況,解決方法是,在傳送前將中文進行編碼,然後後臺再進行解碼:
前臺編碼
var requestData =window.encodeURI("要傳送的中文內容")
;
`$.ajax({
url: 'http://localhost:8080/index/putData',
type: 'GET', //GET
async: false, //或false,是否異步
//contentType:"application/x-www-form-urlencoded;charset=UTF-8",
data: {
attr: requestData
},
timeout: 5000, //超時時間
success: function (data) {
console.log(data);
responseData=data;
sendResponse(responseData);
}
})
`
ajax請求只需一次編碼,如果是get請求,可能還需要再進行一次編碼:
str=encodeURI(str)
後臺解碼
String attr=httpServletRequest.getParameter("arrt");
attr= URLDecoder.decode(attr,"UTF-8");