我有以下 Controller :

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。有谁知道为什么会这样?
提前谢谢!

最佳答案

我觉得应该是:

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

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

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

关于javascript - AngularJS 使用 $http.put() 将表单数据发送到服务器时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20808062/

10-11 22:36
查看更多