搜尋

首頁  >  問答  >  主體

angular.js - Angularjs: Unexpected token g in JSON at position 0 這錯誤怎麼了

錯誤訊息:當我執行saveUser這個方法的時候,就彈出這個錯誤,但是,資料能夠寫進資料庫。這是怎麼回事?

angular1.5.3.min.js:116 SyntaxError: Unexpected token g in JSON at position 0
    at Object.parse (native)
    at uc (http://localhost:8080/js/angular1.5.3.min.js:17:6)
    at ac (http://localhost:8080/js/angular1.5.3.min.js:90:253)
    at http://localhost:8080/js/angular1.5.3.min.js:91:164
    at q (http://localhost:8080/js/angular1.5.3.min.js:7:355)
    at ed (http://localhost:8080/js/angular1.5.3.min.js:91:146)
    at c (http://localhost:8080/js/angular1.5.3.min.js:92:403)
    at http://localhost:8080/js/angular1.5.3.min.js:128:305
    at m.$eval (http://localhost:8080/js/angular1.5.3.min.js:142:467)
    at m.$digest (http://localhost:8080/js/angular1.5.3.min.js:140:47)(anonymous function) @ angular1.5.3.min.js:116(anonymous function) @ angular1.5.3.min.js:89(anonymous function) @ angular1.5.3.min.js:128$eval @ angular1.5.3.min.js:142$digest @ angular1.5.3.min.js:140$apply @ angular1.5.3.min.js:143g @ angular1.5.3.min.js:95x @ angular1.5.3.min.js:100v.onload @ angular1.5.3.min.js:101

js:

var app = angular.module('MyApp', ['ngResource']);


    app.factory('baseRequest', ["$resource", function ($resource) {

        return $resource("/apis/:id/users.req", {id: '@id'}, {

            query: {method: 'get', isArray: false}
        });

    }]);


    app.controller("MyCtrl", ["$scope", "baseRequest", function ($scope, baseRequest) {


        /**
         *   all
         *
         **/

        $scope.fetchAllUsers = function () {

            $scope.users = baseRequest.query();
        };


        /**
         *   first show
         *
         **/

        $scope.fetchAllUsers();

        /**
         *   delete
         *
         **/

        $scope.deleteUser = function (id, name) {

            if (confirm("delete[" + name + "]?")) {

                baseRequest.delete({id: id});
                $scope.fetchAllUsers();

            }
        };

        /**
         *   save
         *
         **/
        $scope.saveUser = function (u) {

            baseRequest.save(u);
            $scope.fetchAllUsers();

        };

    }]);

jsp檔:

<p style="float: left;margin-left: 20px;width: 350px;">

    <span>UserName:</span><input type="text" ng-model="user.name" required><br>
    <span>Password:</span><input type="password" ng-model="user.password" required><br>
    <span>Email:</span><input type="email" ng-model="user.email" required><br>
    <input type="button" ng-click="saveUser(user)" value="save_data"><br>
    <span>user'content:{{user}}</span>

</p>
为情所困为情所困2744 天前820

全部回覆(2)我來回復

  • 習慣沉默

    習慣沉默2017-05-15 17:06:19

    json取出時沒有做小括號的處理。 。 應該是這樣。 。

    回覆
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-15 17:06:19

    定位是你的Json回傳資料格式有誤


    Angularjs要求的Json格式比較嚴格:
    下面兩種都是錯誤範例

    {test: 1} (test 没有使用双引号包裹).
    {'test': 1} ('test' 用了单引号而不是双引号包裹).
    

    正確範例是:

    {"test": 1} 要用双引号包裹

    回覆
    0
  • 取消回覆