JQuery 在提送資料時使用的是 UTF-8 編碼 , 可是客戶要的網站又是 BIG5 編碼 , 真是頭痛死人了 , 再加上突然之間對於那 JSON (JavaScript Object Notation) 的資料傳送方式十分感興趣 , 就硬著頭皮下去衝了呀  
首先遇到的問題就是要怎麼用 JSON 格式來 POST 表單 , 查了一下官方資料 , 先看方法 :  
jQuery.post( url, [data], [callback], [type] )  
嗯嗯 , 好像很簡單 , 試了一下 , 真的很容易調用 , 但....呀!! 怎麼沒有錯誤的處理 , 所以還是使用 JQuery.Ajax 的原型呼叫方法吧 .  
jQuery.ajax( options )  
再改一改 , 我想我需要的就始這些 option 就夠了吧  
$.ajax({ 
    type : "post" , 
    url  :  post_url ,  
    data :  post_str , 
    dataType: "json" , 
    success : function(data){}, 
    error: function (XMLHttpRequest, textStatus, errorThrown){} 
}); 
這時候開始抓出表單要提送的資料來餵給這隻程式看看 , 耶 , 不對呀 , 要提供JSON 格式的資料 , 所以就寫了一個專門轉資料的函數吧  
function getJson_FormValue(form){ 
  var str=\'\'; 
  var d_name,d_data; 
  
  $(this).find("input").each( 
    function(){ 
      if($(this).val()!=\'\'){ 
        switch($(this).attr(\'type\')){ 
          case \'radio\': 
          case \'checkbox\': 
            if ($(this).attr(\'checked\')){ 
              d_name = $(this).attr(\'name\'); 
              d_data = ($(this).val()); 
              if(str!=\'\') 
                str+=\',\'; 
  
              str+= \' \' + d_name +\': \' + \'"\' + d_data+ \'"\'; 
            } 
            break; 
          case \'select-one\': 
          case \'text\': 
          case \'password\': 
          case \'hidden\': 
          case \'textarea\': d_name = $(this).attr(\'name\'); 
              d_data = ($(this).val()); 
              if(str!=\'\') 
                str+=\',\'; 
  
              str+= d_name +\' : \' + \'"\' + d_data+ \'"\'; 
            break; 
          default: 
            break; 
        } 
      } 
    } 
  ); 
  
  eval(\'str = {\' + str + \'};\'); 
  return   str; 
} 
傳看看 , 暈 , 可怕的亂碼出現了 , 查一下資料 , UTF-8 編碼是 JQuery.Ajax 認得的編碼 , 沒辦法 , 山不轉路轉 , 只能在 php 那裡將資料轉回 BIG5 的編碼 .  
//設定回傳的JSON 
$json_array = array(\'action\' => $action , 
		\'error\'  => true , 
		\'runobject\' => \'\' , 
		\'msg\'    => \'不允許\操作\', 
		\'url\'    => \'\' );	 
  
//把接收的資料轉成 Big5 
foreach($_POST as $key => $value) 
	$_POST[$key] = trim(iconv("utf-8","big5",$value)); 
  
$MemberID  = $_POST["MemberID"]; 
$MemberPwd = $_POST["MemberPwd"]; 
  
//判斷登入 
if(!($myMember_Login_Data = $myMember->check_member_login($MemberID,$MemberPwd))){ 
	//回傳錯誤訊息 
	$json_array[\'error\'] = true; 
	$json_array[\'msg\'] = \'登入的帳號密碼錯誤\'; 
}else{ 
	//回傳登入訊息 
	$json_array[\'error\'] = false; 
	$json_array[\'msg\'] = \'歡迎您的登入\'; 
	$json_array[\'url\'] = \'member.php\'; 
  
	//Session控制 
	$_SESSION["Web_Member_Login"] = "login"; 
	$_SESSION["Web_Member_MemberID"] = $MemberID; 
} 
  
//轉成 UTF-8 
$json_array[\'msg\'] = iconv(\'big5\',\'utf-8\',$json_array[\'msg\']); 
  
//印出JSON資料 
echo json_encode($json_array); 
exit;  
在 JQuery.Ajax 中的 Callback 函數處理  
success :  
            function(data){ 
              //將 input 都取消鎖定 
              $(this).find("input").each( 
                function(){ 
                  $(this).attr("disabled",""); 
                } 
              ); 
  
              //處理傳回的資料 
              if(data.error == true){ 
                alert(data.msg); 
                if(data.runobject != \'\'){ 
                  $(this).find("input[name=\'" + data.runobject + "\']").focus(); 
                  if(data.runobject == \'authnum\'){ 
                    //重新獲得一次驗證碼 
                  } 
                } 
                return false; 
              }else{ 
                //轉頁 
                alert(data.msg); 
                document.location.href = data.url; 
                return false; 
              } 
  
            } 
  |