2011-05-29 76 views
0

如何在ajax請求中編碼/解碼utf8?

我的頁面是用iso-8859-1編碼的,但是在發現響應並再次解碼時,如何輕鬆地將數據編碼爲utf8?

function Ajax(){ 
    var _this = this; 

    this.url = null; 
    this.data = null; 
    this.success = null; 

    this.global = true; 
    this.timeout = JSON_TIMEOUT; 
    this.cache = false; 
    this.dataType = 'json'; 
    this.type = 'post'; 

    this.send = function(){ 
     var jqxhr = $.ajax({ 
       url : this.url, 
       data : this.data, 
       timeout : this.timeout, 
       cache : this.cache, 
       dataType : this.dataType, 
       type : this.type, 
       global : this.global 
       } 
      ) 
      .success(this.success) 
      .error(function(){ 
       // something 
      }) 
      .complete(function(){ 

      }); 
    }; 
} 

回答

1

你不需要做任何事情。 jQuery的ajax()函數將始終以UTF-8傳輸數據。

無論你的頁面被編碼在哪裏,因爲Javascript總是在內部使用Unicode並正確翻譯你的iso-8859-1頁面。

0

我認爲你需要使用的格式如下:

$.ajax({ 
    type: "POST", 
    url: "WebMethodName", 
    data: {'fname':'dave', 'lname':'ward'}, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json" 
}); 
+0

jQuery的ajax()調用將始終以UTF-8編碼傳輸數據。不管你的頁面編碼的是什麼,編碼的是什麼statis文件,因爲JavaScript總是將當前的編碼轉換爲Unicode。 – Codo 2011-05-29 19:05:50

0

我解決了這個問題分兩個步驟... 首先,在被稱爲「php_function.php」 PHP函數,包括我在每一頁上的文件,我添加了這個片斷:

「php_function.php」 :

<?php 
/*----------------------------------------------------------------------------- 
Codification($string) 
$string = String to be consulted 
Returns which encoding used in string 
-----------------------------------------------------------------------------*/ 
function Codification($string) { 
    return mb_detect_encoding($string.'x', 'UTF-8, ISO-8859-1'); 
} 

/*----------------------------------------------------------------------------- 
Updates all parameters sent from any page. 
-----------------------------------------------------------------------------*/ 
foreach($_GET as $key => $value) 
    $_GET[$key] = (Codification($value)=='UTF-8') ? utf8_decode($value):$value; 
foreach($_POST as $key => $value) 
    $_POST[$key] = (Codification($value)=='UTF-8') ? utf8_decode($value):$value; 

/*----------------------------------------------------------------------------- 
GetPost($string) 
$ string = name of the variable used 
Receives the name of the variable and tries to get by Post and/or GET 
Returns the value of the variable ... 
-----------------------------------------------------------------------------*/ 
function GetPost($GetPost) { 
    $Ret = $_GET[$GetPost]; 
    if(trim($Ret) == '') 
    $Ret = $_POST[$GetPost]; 
    return $Ret; 
} 
?> 

其次,我稱之爲 'php_function.php' 在我的請求的頁面:

'requested_pa​​ge.php':

<?php 
require_once('php_function.php'); 
//... Get vars here 
$var1 = GetPost('var1'); 
echo $var1; 
//... Other implementations 
?> 

發件人頁面是一樣的東西:

'的index.php':

<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
</head> 
<body> 
    <form name="form1" method="post" type="multipart/form-data"> 
    <input type="text" name="var1"> 
    <input type="submit" name="Send" id="Send" value="Send"> 
    </form> 
    <script> 
    $(function(){ 
     Ajax = function(params) { 
     $.ajax({ 
      url: 'requested_page.php', dataType: 'html', type: 'POST', 
      data: params, async: false, 
      error: function(e) { alert("Error: "+e.responseText); }, 
      success: function(data) { alert(data); }, 
      complete: function() {} 
     }); 
     } 
     $("form").on("submit", function(e) { 
     e.preventDefault(); 
     var params = $(this).serialize(); 
     Ajax(params); 
     }); 
    }); 
    </script> 
</body> 
</html> 

通過發送文本,如 'aáàéêeoóu' 用ajax,沒有編纂功能打印 'aáÃéêeoóu' (由ajax編碼的UTF-8),編碼功能打印'aáàéêeoóu'!