Jquery utf8/iso-8859-1编码i$.ajax()

Jquery utf8/iso-8859-1编码i$.ajax(),jquery,Jquery,如何在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.cac

如何在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(){

            });
    };
}

我认为您需要使用如下格式:

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

你不需要做任何事。jQuery的ajax()函数将始终以UTF-8格式传输数据


你的页面编码成什么并不重要,因为Javascript总是在内部使用Unicode,并正确地翻译你的iso-8859-1页面。

我分两步解决了这个问题。。。 首先,在每个页面上都包含一个名为“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”:

“请求的_page.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>

$(函数(){
Ajax=函数(params){
$.ajax({
url:'requested_page.php',数据类型:'html',类型:'POST',
数据:params,异步:false,
错误:函数(e){alert(“error:+e.responseText);},
成功:函数(数据){警报(数据);},
完成:函数(){}
});
}
$(“表格”)。关于(“提交”,职能部门(e){
e、 预防默认值();
var params=$(this.serialize();
Ajax(params);
});
});
通过使用ajax发送类似“aáêeoóu”的文本,不使用编码功能打印“aÃÃÃÃÃÃÃÃóu”(由ajax编码的UTF-8),并使用编码功能打印“aáþeoóu”

jQuery的ajax()调用将始终以UTF-8编码传输数据。无论您的页面编码是什么,statis文件编码是什么,因为JavaScript总是将当前编码转换为Unicode。