javascript-使用$http.put()将表单数据发送到服务器时出现AngularJS错误

我有以下控制器:

function EditCtrl($scope,$http,$routeParams,$location) {
    $scope.master = {};
    $scope.actviePath = null;
    $http.get("/employeeApp/assets/php/index.php/users/" + $routeParams.id).success(function (data) {
        $scope.users = data;

        $scope.user = {
            name: data.name,
            email: data.email,
            userName: data.userName,
            password: data.password,
            role: data.role,
            availability: data.availability,  
        };
    });

     $scope.update_user = function(user) {

    $http.put("/employeeApp/assets/php/index.php/users/"+$routeParams.id, user).success(function(){
      $scope.reset();
      $scope.activePath = $location.path('/');
    });
    $scope.reset = function() {
      $scope.user = angular.copy($scope.master);
    };
    $scope.reset();
    };
}

和以下html模板:

<div ng-init="$root.title='Add new user'">
    <h2>Edit user: </h2>
    <form novalidate name="AddNewForm" id="add-new-form" method="post" action="">
        <label for="name">Name:</label>
        <input type="text" ng-model="user.name" />
        <label for="email">Email:</label>
        <input type="email" ng-model="user.email" />
        <label for="username">Username:</label>
        <input type="text" ng-model="user.userName" />
        <label for="password">Password:</label>
        <input type="password" ng-model="user.password" />
        <label for="role">User Role:</label>
        <input type="text" ng-model="user.role" />
        <label for="availability">Availability: </label>
        <input type="text" ng-model="user.availability" />
        <!--<input type="hidden" ng-model="user.id" />-->
        <br />
        <button class="btn btn-primary"  id="add-new-btn" ng-click="update_user(user)">Save!</button>
        <a href="#/" class="btn">Cancel</a>

    </form>
</div>

当我尝试编辑用户然后保存更改时,控制台中会出现以下错误,并且表单数据未发送到服务器:

SyntaxError: Unexpected token S
    at Object.parse (native)
    at fromJson (angularjs/1.2.6/angular.js:1035:14)
    at $HttpProvider.defaults.defaults.transformResponse (angularjs/1.2.6/angular.js:6926:18)
    at angularjs/1.2.6/angular.js:6901:12
    at Array.forEach (native)
    at forEach angularjs/1.2.6/angular.js:302:11)
    at transformData angularjs/1.2.6/angular.js:6900:3)
    at transformResponse (angularjs/1.2.6/angular.js:7570:17)
    at wrappedCallback (angularjs/1.2.6/angular.js:10905:81)
    at angularjs/1.2.6/angular.js:10991:26 angular.js:9383
(anonymous function) angular.js:9383
(anonymous function) angular.js:6825
wrappedCallback angular.js:10908
(anonymous function) angular.js:10991
Scope.$eval angular.js:11906
Scope.$digest angular.js:11734
Scope.$apply angular.js:12012
done angular.js:7818
completeRequest angular.js:7991
xhr.onreadystatechange

在服务器上,我有一个用slim做的php API.到服务器的链接是app / index.php / user / suerId.
当我尝试输出{{user}}时,它会正确显示数据,但是我不知道为什么无法将数据发送到服务器.
我正在使用angularjs 1.2.6.有谁知道为什么会这样吗?
提前thnx!

解决方法:

我认为应该是:

$http.put("/employeeApp/assets/php/index.php/users/"+$routeParams.id,
  $scope.user)

代替

$http.put("/employeeApp/assets/php/index.php/users/"+$routeParams.id,
  user)

您收到的错误是angularjs无法将用户解析为JSON.解析$scope.user应该没有问题.

上一篇:基于slim的神经网络模型的训练-笔记


下一篇:PHP-Slim Framework 404 CSS问题