首頁 >後端開發 >php教程 >angularjs post 提交中文亂碼?

angularjs post 提交中文亂碼?

WBOY
WBOY原創
2016-09-15 11:30:541256瀏覽

html:

<code><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></code>

php:

<code><?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;
}
?>
</code>

接收到的user_name是亂碼,這裡是需要設定post時的headers嗎?應該如何設定。
出於各種原因,需要最大限度地兼容以前的系統,所以要用gb2312.

回覆內容:

html:

<code><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></code>

php:

<code><?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;
}
?>
</code>

接收到的user_name是亂碼,這裡是需要設定post時的headers嗎?應該如何設定。
出於各種原因,需要最大限度地兼容以前的系統,所以要用gb2312.

可以貼下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);

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn