【问题标题】:Post method throws an errorpost方法抛出错误
【发布时间】:2015-03-08 19:53:21
【问题描述】:

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

index.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>
...

controllers.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)

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

【问题讨论】:

  • 因为你的成功函数只有一个参数?也拼错了succes

标签: javascript ajax angularjs angularjs-http misspelling


【解决方案1】:

拼写错误的success,写成succes

【讨论】:

    【解决方案2】:

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

    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')
                    });
            };
        }]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多