angularjs post 提交中文乱码?

html:

<div  ng-controller="login_input">
    <form ng-submit="check_login()">
        <div ><a>用户名:</a></div><div ><input type="text" required ng-model="login.uname"/></div>
        <div ><a>密码:</a></div><div ><input type="password" required ng-model="login.upassword"/></div>
        <div ><a>验证码:</a></div><div ><input type="number" min="0" required ng-model="login.chkcode"/><img src="img.php"/></div>
{{login}}
        <button class="a_button" type="submit">登录</button>
    </form>
</div>
</body>
<script type="text/javascript" src="js/angular.min.js"></script>
<script>
    var app = angular.module('login', []);

    app.controller('login_input', function ($scope,$http) {
      $scope.check_login=function(){
          var aa  = {
              uname : $scope.login.uname,
              upassword:$scope.login.upassword,
              chkcode:$scope.login.chkcode
          }
         if($scope.login.uname&&$scope.login.upassword&&$scope.login.chkcode){
             $http({
                 method: 'POST',
                 url: 'login.php',
                 data: aa,
             })
                 .success(function(response){
                     console.log(response);
                 })
         }
      }
    });
</script>

php:

<?php
header('Content-type: text/html; charset=gb2312');
$params = json_decode(file_get_contents('php://input'), true);

require("cfg.php");
global $dbh;
$user_name= $params["uname"];
$user_password= $params["upassword"];
$user_chkcode= $params["chkcode"];
var_dump(is_register());
var_dump($user_name);
function is_register()
{
    global $dbh, $user_name, $user_password;
    $up = sha1($user_password);

    $sql = "select user from gggg";
    $sth = $dbh->prepare($sql);
    $sth->bindParam(':username', $user_name);
   // $sth->bindParam(':pwd', $up);
    $sth->execute();
    $rs = $sth->fetchAll(PDO::FETCH_ASSOC);

      return $rs;
}
?>

接收到的user_name是乱码,这里是需要设置post时的headers吗?应该如何设置。
出于各种原因,需要最大限度地兼容以前的系统,所以要用gb2312.

阅读 6.5k
2 个回答

可以贴下post请求的http信息吗,

应该可以接收的时候先用UTF-8接收,然后要存储或者与其他服务交互在服务端转成gb2312,使用iconv()

@黎明星刻 提供的博客文章 http://blog.csdn.net/vera_xue...

我在php中使用如下代码解决了问题:
$params = json_decode(file_get_contents('php://input'), true);
require("cfg.php");
global $dbh;
$user_name = $params["uname"];//utf-8
$user_name = iconv("UTF-8", "GB2312", $user_name);

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题