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

标签 javascript php http angularjs slim

我有以下 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/

相关文章:

javascript - 将 HTML5 数据类型转换为数字

http - 来自 golang 的经过身份验证的 http 客户端请求

javascript - 在javascript中分配另一个键的值

javascript - 根据单击的按钮将相同的表单发布到不同的操作

php - 如果小图片出现在大图片中,我该如何检测/计算?

php - 如何使用Tmail发送邮件?

http - 代理和记录 http 流量的单行程序?

php - 重试模式,如何确定 PHP 中的失败类型

javascript - Magento - 无法在管理员中上传产品图片

javascript - 将 jQuery 脚本与 WordPress foreach 循环结合使用