萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> js ajax中傳遞中文參數處理

js ajax中傳遞中文參數處理

   ajax中傳遞中文參數處理代碼:

 代碼如下  

js中:

send_request('http://www.45it.com /mini_do.php?username=' + encodeURI(username) + '&phone=' + encodeURI(phone)+ '&content=' + encodeURI(content));

mini_do.php中:

$username = urldecode($_REQUEST[username]);
$phone = urldecode($_REQUEST[phone]);
$content = urldecode($_REQUEST[content]);

  最後有朋友說可以使用escape函數

  var htmer ="getcode="+escape(getcode)+"&Content="+escape(Content);

  這個本人不建義哦因為它出錯更高有些漢字字符不能識別,還有一個函數encodeURIComponent這個比較全面了

  encodeURIComponent,它是將中文、韓文等特殊字符轉換成utf-8格式的url編碼,所以如果給後台傳遞參數需要使用encodeURIComponent時需要後台解碼對utf-8支持(form中的編碼方式和當前頁面編碼方式相同)

  總結

  從上面來看

  escape不編碼字符有69個:*,+,-,.,/,@,_,0-9,a-z,A-Z

  encodeURI不編碼字符有82個:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z

  encodeURIComponent不編碼字符有71個:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z

  編碼范圍來看我們選擇encodeURI是最佳的選擇了。

copyright © 萬盛學電腦網 all rights reserved