Php 错误:StringServices.insertString(…)未定义

Php 错误:StringServices.insertString(…)未定义,php,angularjs,Php,Angularjs,我是angularjs新手,我正在做基本的演示,我正在使用php和angularjs服务将数据插入数据库并将其传递给控制器,数据正在插入数据库,但控制台日志中出现错误。有人能帮我解决这个错误吗 这是我的app.js index.html 你的名字 insert.php 我得到了这个错误 StringServices中的insertString函数只接受一个为User的参数,但是在控制器中传递两个参数,即User和一个函数。因此,没有具有两个参数的函数insertString 你可以有这样

我是angularjs新手,我正在做基本的演示,我正在使用php和angularjs服务将数据插入数据库并将其传递给控制器,数据正在插入数据库,但控制台日志中出现错误。有人能帮我解决这个错误吗

这是我的app.js index.html

你的名字
insert.php

我得到了这个错误

StringServices中的insertString函数只接受一个为User的参数,但是在控制器中传递两个参数,即User和一个函数。因此,没有具有两个参数的函数insertString

你可以有这样的东西:

var app = angular.module('myApp', [])
//controller
app.controller("myController",['$scope','StringServices', function($scope,StringServices){
    $scope.User= {};
    $scope.insert = function(User){
        StringServices.insertString($scope.User, function(response){
            if(response.FLAG === "_SUCCESS"){
                console.log('Success');
            }
            else{
                console.log('Error');
            }
        });
    }
}])
//services

app.factory('StringServices', function($http){
    return {
        insertString: function(User, callbackFn){
            var data = {name: User.name};

            $http.post('http://localhost/anjali_services/server/insert.php',data)
            .success(callbackFn);
        }
    };
});

我不得不说,我无法立即找出您问题的根源,但我建议您至少从您的服务中返回
$http
承诺,并使用
then/catch
处理结果。至少我觉得这更容易阅读和理解正在发生的事情

无论如何,以这种方式修改示例似乎效果不错。不管怎样,还是反对模拟REST服务

HTML

<body ng-controller="myController as vm">
  Your Name:
  <input type= "text" ng-model="User.name">
  <input type="button" ng-click="insert(User)" value="Insert">
</body>

这里的相关plunker

感谢您的回复,我正在使用StringService和insertString函数,我已经正确地定义了它们,但在控制台中,我发现了一个错误,即没有定义StringService。所以..您能建议我该怎么做吗..???@Sohlowmawn您用第二个参数误解了回调。@ravishankar他调用的是一个带有一个参数的函数,回调函数也需要作为参数传递。函数只需要一个参数,他传递了两个参数,因此angular js无法找到这样的函数。很抱歉,但这是不正确的(据我所知):此函数接收一个参数,仅此而已。忽略传递给JS中函数的其他参数(除非您使用
参数
对象)。^问题是,如果您尝试按原样运行代码,则未定义
错误
。我不明白这个答案是如何解决的(我也不知道)。
<?php 
    $db = new PDO("mysql:host=localhost;dbname=anjali;port=3306","root","");
    $data = json_decode(file_get_contents("php://input"));
    $name = $data->name;
    $resp = array();
    $q = "INSERT INTO udata (name) VALUES (:name)";
    $query = $db->prepare($q);
    $execute = $query->execute(array(
        ":name" => $name
    ));
    if($execute == true){
        $resp['FLAG'] = "_SUCCESS";
        print json_encode($resp);
    }else{
        echo "ERROR";
    }

?>
var app = angular.module('myApp', [])
//controller
app.controller("myController",['$scope','StringServices', function($scope,StringServices){
    $scope.User= {};
    $scope.insert = function(User){
        StringServices.insertString($scope.User, function(response){
            if(response.FLAG === "_SUCCESS"){
                console.log('Success');
            }
            else{
                console.log('Error');
            }
        });
    }
}])
//services

app.factory('StringServices', function($http){
    return {
        insertString: function(User, callbackFn){
            var data = {name: User.name};

            $http.post('http://localhost/anjali_services/server/insert.php',data)
            .success(callbackFn);
        }
    };
});
<body ng-controller="myController as vm">
  Your Name:
  <input type= "text" ng-model="User.name">
  <input type="button" ng-click="insert(User)" value="Insert">
</body>
var app = angular.module('myApp', [])
  .controller('myController', function($scope, StringServices) {
    $scope.User = {};

    $scope.insert = function(User) {
      StringServices.insertString(User)
        .then(function(response) {
          console.log('ok', response);
        })
        .catch(function(error) {
          console.log('failed', error);
        });
    };
  })
  .factory('StringServices', function($http){
    return {
      insertString: function(User){
        return $http.post('https://httpbin.org/post', { name: User.name });
      }
    };
});