success 函数在 http post 请求中不起作用 - AngularJS

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

  • register.php有登记表。

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

  • sqlregister.php具有 INSERT 查询。

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

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

注册.php:

<!doctype html>

<html 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>

maincons.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";

}

?>

问题是成功函数不提醒数据。


炎炎设计
浏览 55回答 2
2回答

白板的微信

根据doc&nbsp;这应该是语法var req = {method: 'POST',&nbsp;url: 'http://example.com',headers: {&nbsp; 'Content-Type': undefined&nbsp;},data: { test: 'test' }}$http(req).then(function(res){&nbsp;&nbsp; &nbsp; console.log(res);&nbsp;}, function(err){&nbsp; &nbsp; &nbsp;console.log(err);&nbsp;});

郎朗坤

$http.post(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "sqlregister.php", {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'fname': $scope.fname,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email': $scope.email&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; )此代码不会像您在 PHP 中期望的那样发送 JSON。它以格式发送数据www-form-urlencoded。您可以在 PHP 中直接从 $_POST 超全局数组访问此数据。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5