2017-01-25 8 views
-2

私はエラーTypeError: t(...).success is not a functionを受け取りました。私はいくつかの検索をしましたが、なぜ私のケースでこのエラーが発生するのか理解できました。TypeError:t(...)。成功は関数ではありません - 角度

私のJSコードは以下のようになります。私は間違って何をしていますか?なぜこのエラーが発生するのですか?

var app = angular.module('PriceListEditModule', ['ngRoute']); 
app.controller('PriceListEditController', ["$scope", "$q", function ($scope, $q) { 

    GetBrands(); 

    function GetBrands() { 
     $http({ 
      method: 'Get', 
      url: '/GetBrands' 
     }).success(function (data, status, headers, config) { 
      $scope.brands = data; 
     }).error(function (data, status, headers, config) { 
      $scope.message = 'Unexpected Error'; 
     }); 
    } 
}]); 

とエラーが角度がHTTPでの成功を持っていない

jquery:1 TypeError: t(...).success is not a function 
    at i (jquery:1) 
    at new <anonymous> (jquery:1) 
    at Object.invoke (jquery:1) 
    at v.instance (jquery:1) 
    at pt (jquery:1) 
    at p (jquery:1) 
    at p (jquery:1) 
    at jquery:1 
    at jquery:1 
    at p.$eval (jquery:1) 
+0

なぜdown vote ??縮小コードのエラー –

+2

は役に立ちません。バージョン番号は便利です。 –

+0

私は何をすべきですか? –

答えて

1

以下のようなものです。

$http documentation

$http({ 
    method: 'GET', 
    url: '/someUrl' 
}).then(function successCallback(response) { 
    // this callback will be called asynchronously 
    // when the response is available 
    }, function errorCallback(response) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
    }); 

そして、あなたはあなたのコントローラ内の参照として$ HTTPを含めていません。

app.controller('PriceListEditController', 
    ["$scope", "$q", "$http", function ($scope, $q, $http) { 

あなたは本当にhttpコールのためにサービスを使用していて、コントローラでそれをしていないはずです。 ($ HTTPが定義されている)あなたのサービスで

+0

ありがとうございます。今私はそれを持っている –

0

変更して、方法

getBrands = function(){ 
    $http.get('/GetBrands', {}) 
     .then(function (result) { 
      return result.data; 
     }); 
} 

を作成し、

getBrands(data).then(function(data){ 
    //do some error checking and then assign 
    $scope.brands = data; 
}) 

角度HTTPを使用して、コントローラから呼び出すですjQueryのajax呼び出しと少し違います。あなたが投稿したコードは、角度のサービス呼び出しではなく、ajax呼び出しと非常によく似ています。 Angularはこの構文でコールバックをサポートしていません。

+1

それdoesnt仕事。 –

+0

エラーは何ですか?あなたは$ httpを宣言しましたか? –

+0

jquery:1 ReferenceError:$ httpが定義されていません –

0

角度のバージョンを確認してください。

$http({ 
    url: "/url", 
    method: 'GET', 
}).success(function (result) { 
    //do success 
}).error(function (data, status, headers, config) { 
    //do error 
}); 

それとも、バージョン1.6.0以降を持っている場合に使用します:

を自分 v1.5.11 documentation

Deprecation Notice

The $http legacy promise methods success and error have been deprecated and will be removed in v1.6.0. Use the standard then method instead.

によると、これは以前のバージョンを持っている場合は、のようなものを使用すること

$http({ 
    url: "/url", 
    method: 'GET', 
}).then(function successCallback(response) { 
    // this callback will be called asynchronously 
    // when the response is available 
    }, function errorCallback(response) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
    }); 

$httpが定義されていない場合は、コントローラに注入しませんでした。コントローラーに次のように注入してください。

app.controller('controller', ['$scope','$http', function($scope,$http){}]) 
関連する問題