【问题标题】:success function dont work in http post request - AngularJS成功功能在http post请求中不起作用 - AngularJS
【发布时间】:2020-07-23 06:54:47
【问题描述】:

我试图发出一个基本的 http post 请求,将数据插入到我的数据库中。 我有页面:

  • register.php有注册表。

  • maincons.js 拥有应用程序的控制器。

  • sqlregister.php 具有 INSERT 查询。

我正在使用 MeekroDB(sql 函数库)。

应用程序将数据插入数据库,但成功功能不起作用。

register.php:

<!doctype html>
<html lang="he" dir="rtl">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../css/style.css">
    <script src="../js/angular.min.js"></script>

    <title>register</title>
  </head>
  <body>
  <div ng-app="appRegister" ng-controller="conRegister" class="grid">
  <form name="register" method="post" class="col-4 regform" ng-submit="valfunc()">
  <input  type="text" name="fname" ng-model="fname"  required>
  <input  type="email" name="email" ng-model="email"  required>
  <input type="submit" value="register">
  </form>  

</div>
<script src="../js/maincons.js"></script>
  </body>
</html>

ma​​incons.js:

var app = angular.module('appRegister', []);
app.controller('conRegister', function($scope,$http) {
    $scope.valfunc = function () {
            $http.post(
                "sqlregister.php", {
                    'fname': $scope.fname,
                    'email': $scope.email
                }
            ).success(function(data){
                alert(data);
              })
              .error(function(data){
                alert(data);
              })

    }
});

sqlregister.php:

<?php
require_once '../system/sqlfunc.php';
$info = json_decode(file_get_contents("php://input"));
if(count($info) > 0) {
    $fname = $info->fname;
    $email = $info->email;
    $query=DB::insert('tbcustomers', [
        'Custname' => $fname,
        'email' => $email
      ]);
      if($query==1)
      echo "true";
      else
      echo "false";
}
?>

问题是成功函数没有提醒数据。

感谢帮助。

【问题讨论】:

标签: javascript php html angularjs


【解决方案1】:

根据doc,这应该是语法

var req = {
method: 'POST',
 url: 'http://example.com',
headers: {
  'Content-Type': undefined
 },
data: { test: 'test' }
}

$http(req)
.then(function(res){ 
    console.log(res);
 }, function(err){
     console.log(err);
 });

【讨论】:

    【解决方案2】:
    $http.post(
                "sqlregister.php", {
                    'fname': $scope.fname,
                    'email': $scope.email
                }
            )
    

    此代码不会像您在 PHP 中所期望的那样发送 JSON。它以www-form-urlencoded 格式发送数据。

    您可以在 PHP 中直接从 $_POST 超全局数组访问这些数据。

    【讨论】:

      猜你喜欢
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多