AngularJS“TypeError:无法读取未定义的属性'协议'”从Web服务获取发布数据时

时间:2015-04-25 18:35:54

标签: javascript angularjs http

我正在尝试从Web服务获取数据。这是我的代码。

$scope.savePricing = function(){
        var pricing = {
          "PricingSchemeId": 0,
          "Name": $scope.name,
          "LastUserName": "Tan",
          "LastModifiedDate": "",
          "IsActive": true,
          "CurrencyId": $scope.selectedCurrency.CurrencyId,
        }
            $http({
                url: config.addPricing,
                data: pricing,
                method: "POST"
            }).success(function(data){
                alert("Success");
            }).error(function(data){
                alert("Failed");
            });
    })

但是当我运行此功能时,会显示以下错误: enter image description here

我现在能做什么?

1 个答案:

答案 0 :(得分:0)

config.addPricing未定义。我可以轻松地重现该错误如下:

index.html

<!DOCTYPE html>
<html ng-app="Foo">
  <head>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script>
    <script src="my-angular.js"></script>
  </head>
  <body ng-controller="SomeCtrl">
    <button ng-click="go()">Go</button>
  </body>
</html>

my-angular.js

var app = angular.module("Foo", []);
app.controller("SomeCtrl", function($scope, $http) {
  var config = {}; // Note the lack of "url" field.
  $scope.go = function() {
    $http({
      url: config.url, // undefined
      data: {},
      method: "POST"
    }).success(function(data) {
      console.log("Success: data", data);
    }).error(function(data) {
      console.log("Error: data", data);
    });
  };
});