今天遇到一個(gè)問題,有關(guān)ajax 請(qǐng)求中傳輸中文,遇到亂碼的問題。
如下代碼:
function UpdateFolderInfoByCustId(folderId, folderName, custId) { $.ajax({ type: "Post", contentType: "application/x-www-form-urlencoded; charset=utf-8", url: "http://localhost/CRM/Ashx/HandKBSucessCustomer.ashx?Method=UpdateCustomerByCustId&folderId=" + folderId + "&folderName=" + encodeURI(encodeURI(folderName)) + "&custId=" + custId, success: function (msg) { alert(msg); }, error: function (error) { alert(error); } }); }
如上代碼 如果只是傳“&foderName=”+folderName 的話,漢字就會(huì)產(chǎn)生亂碼,如果經(jīng)過encodeURL 轉(zhuǎn)換兩次的話,漢字編碼會(huì)變?yōu)轭愃?/p>
“%e6%b5%8b%eb%af%95” 的格式。轉(zhuǎn)換為這種格式之后,在獲取的時(shí)候在轉(zhuǎn)碼,如下所示:
public void UpdateCustomerByCustId() { int folderId = Convert.ToInt32(Request["folderId"]); string folderName = Request["folderName"]; string folderName2 = Convert.ToString(System.Web.HttpUtility.UrlDecode(folderName)); int custId = Convert.ToInt32(Request["custId"]); bool res = false; try { res = CustomerBusiness.UpdateCustomerByCustId(folderId, folderName2, custId); } catch (Exception ex) { throw; } Response.Write(res); } } }
經(jīng)此轉(zhuǎn)換之后,即可獲取到傳輸?shù)臐h字。
以上所述是小編給大家介紹的關(guān)于Ajax請(qǐng)求中傳輸中文亂碼問題的解決方案,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com