0

我正在尝试基于教程 https://docs.angularjs.org/tutorial/step_00中的应用程序做一个测试应用程序。它工作正常,但我有 post 方法。

索引.html

...
<div class="control_panel" ng-controller="phonecatControllers">
            <button class="btn btn-default" ng-click="chiliSpicy()">Chili!</button>
            <button class="btn btn-default" ng-click="sendData()">send!</button>
</div>
...

控制器.js

'use strict';

var phonecatControllers = angular.module('phonecatControllers', []);

phonecatControllers.controller('PhoneListCtrl', ['$scope', '$http', '$log',
    function ($scope, $http, $log) {
        $http.get('http://localhost:8080/webapp/rest/myresource/posts').
            success(function (data) {
                $scope.posts = data;
            });

        $scope.data = "hello world";

        $scope.chiliSpicy = function () {
            $log.info('chili function');
        };

        $scope.sendData = function () {
            $http.post('http://localhost:8080/webapp/rest/myresource/',  {'data' : $scope.data} )               
                .succes(function (data, status, headers, config) {  // !!! here is line 39
                    $log.info('sent');
                })
                .error(function (data, status, headers, config) {
                    $log.error('not sent')
                });
        };
    }]);

Get 方法可以正常工作(显示的 html 代码不包含它的用法),chiliSpicy 函数也可以工作。但是 sendData 函数会抛出错误(成功函数在哪里)

 TypeError: undefined is not a function
    at l.$scope.sendData (http://localhost:8080/webapp/controllers.js:39:18)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:198:424
    at chrome-extension://ighdmehidhipcmcojjgiloacoafjmpfk/dist/hint.js:798:21
    at l.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:126:84)
    at l.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:126:310)
    at l.scopePrototype.$apply (chrome-extension://ighdmehidhipcmcojjgiloacoafjmpfk/dist/hint.js:1478:22)
    at HTMLButtonElement.<anonymous> (chrome-extension://ighdmehidhipcmcojjgiloacoafjmpfk/dist/hint.js:797:25)
    at HTMLButtonElement.c (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js:32:363)

实际上,服务器接收到数据,但是成功函数没有通过。任何想法?谢谢。

4

2 回答 2

1

有错别字。它应该是成功而不是“成功”

phonecatControllers.controller('PhoneListCtrl', ['$scope', '$http', '$log',
    function ($scope, $http, $log) {
        $http.get('http://localhost:8080/webapp/rest/myresource/posts').
            success(function (data) {
                $scope.posts = data;
            });

        $scope.data = "hello world";

        $scope.chiliSpicy = function () {
            $log.info('chili function');
        };

        $scope.sendData = function () {
            $http.post('http://localhost:8080/webapp/rest/myresource/',  {'data' : $scope.data} )               
                .success(function (data, status, headers, config) {  // !!! here is line 39
                    $log.info('sent');
                })
                .error(function (data, status, headers, config) {
                    $log.error('not sent')
                });
        };
    }]);
于 2015-01-10T16:40:35.740 回答
1

拼写错误success,写成succes

于 2015-01-10T16:38:03.767 回答