为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

炎炎设计 2023-12-04 17:01:58
我正在尝试发出一个基本的 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:<?phprequire_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";}?>问题是成功函数不提醒数据。
查看完整描述

2 回答

?
白板的微信

TA贡献1883条经验 获得超3个赞

根据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);

 });


查看完整回答
反对 回复 2023-12-04
?
郎朗坤

TA贡献1921条经验 获得超9个赞

$http.post(

            "sqlregister.php", {

                'fname': $scope.fname,

                'email': $scope.email

            }

        )

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


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


查看完整回答
反对 回复 2023-12-04
  • 2 回答
  • 0 关注
  • 49 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信