Home >Backend Development >PHP Tutorial >angularjs post submission Chinese garbled code?

angularjs post submission Chinese garbled code?

WBOY
WBOYOriginal
2016-09-15 11:30:541256browse

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>

The user_name received is garbled. Do you need to set the headers when posting? How it should be set.
For various reasons, it is necessary to maximize compatibility with previous systems, so gb2312 is used.

Reply content:

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>

The user_name received is garbled. Do you need to set the headers when posting? How it should be set.
For various reasons, it is necessary to maximize compatibility with previous systems, so gb2312 is used.

Can you post the http information of the post request?

When it should be received, use UTF-8 to receive it first, and then convert it to gb2312 on the server side if you want to store or interact with other services, use iconv()

Blog article provided by @ Dawn Stars http://blog.csdn.net/vera_xue...

I solved the problem using the following code in 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);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn