Angular Js-Json解析错误

时间:2017-01-18 06:00:10

标签: angularjs json eclipse parsing

解析Json时,我在日志中收到此错误

(function() {
  'use strict';
  var app = angular.module('app');
  app.controller('AddController', AddController);
  function AddController($scope, $http) {
    $scope.mArray = [];
    $scope.clickButton = function() {
      $scope.conn = {
        'deviceToken': '',
        'deviceOs': '',
        'userName': ''
      };
      if ($scope.device_token == "" || $scope.device_os == "" || $scope.user_name == "" || $scope.device_token == undefined || $scope.device_os == undefined || $scope.user_name == undefined) {
        alert("Please fill the fields");
      } else {
        $scope.conn.deviceToken = $scope.device_token;
        $scope.conn.deviceOs = $scope.device_os;
        $scope.conn.userName = $scope.user_name;
        $scope.mArray.push($scope.conn);
        $scope.addDeviceToDb($scope.conn);
        console.log("hai -" + $scope.mArray.length);
        for (var i = 0; i < $scope.mArray.length; i++) {
          console.log($scope.mArray.indexOf($scope.mArray[i]) + 1);
        }
      }
    }

    $scope.PushNotification = function(row) {
      // use $.param jQuery function to serialize data from JSON
      console.log(row);
      var data = JSON.stringify({
        "notification": {
          "title": "Ionic 2 FCM Starter", // Any value
          "body": "new Message", // Any
          // value
          "sound": "default", // If you want notification sound
          "click_action": "FCM_PLUGIN_ACTIVITY", // Must be present
          // for Android
          "icon": "fcm_push_icon" // White icon Android resource
        },
        "data": {
          "param1": "value1"
        },
        "to": row.deviceToken,
        // "cYK3qwfbb54:APA91bGx0kMMvzyKudcjObxZO5slHg5b8PB8wysfdMf_1wlfErkM7XO7sMHj-tL_a9hDkZ-gyPXzr6wm1NWDNIpfcl5xN1vCgESB4HeUyG4pHpjfCeXcKY7th1GRJbMC84zTJAt3fXKT",
        // Topic or single device
        "priority": "high", // If not set, notification won't be
        // delivered on completely closed iOS
        // app
        "restricted_package_name": "" // Optional. Set for application
          // filtering
      });

      var config = {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'key=AAAAgXKnYTY:APA91bFRvdDdqD2vnY3kFQjOAu1lgYVY7DgsxsA9d3p2WOvQp1txbEqnibBDRjfv5duq1SG0NdYZeUNXzg_fI8d4p--9O-po2-ISDwlg4qh3uQdCprLQV5n8thJaIWXOz3eHYSbIdB33FC7x_EjaACBQLb44HflgBQ'
        }
      }

      $http.post('https://fcm.googleapis.com/fcm/send', data, config)
        .success(function(data, status, headers, config) {
          $scope.PostDataResponse = data;
        }).error(
          function(data, status, header, config) {
            $scope.ResponseDetails = "Data: " + data + "<hr />status: " + status + "<hr />headers: " + header + "<hr />config: " + config;
          });
      alert("Message Successfully send");
    }

    $scope.addDeviceToDb = function(data) {

      $http({
        method: 'POST',
        url: "http://10.10.14.54:8080/FcmBackend_ws/rest/add_device",
        headers: {
          'Content-Type': 'application/json'
        },
        transformRequest: function(obj) {
          var str = [];
          for (var p in obj)
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
          return str.join("&");
        },
        data: {
          token: data.deviceToken,
          device_os: data.deviceOs,
          device_version: data.userName
        }
      }).success(
        function(data, status, headers, config) {
          console.log("Successfull data : " + data.toString + " status :" + status);
        }).error(function(data, status, header, config) {
        console.log("Failed data : " + data + " status :" + status);

      });
    }
    var getDeviceFromDb = function(data, $http) {
      var response = $http({
        method: 'GET',
        url: "http://localhost:8080/FcmBackend_ws/rest/devices",
        withCredentials: true,
        headers: {
          'Content-Type': 'application/json; charset=utf-8'
        }
      }).success(
        function(data, status, headers, config) {
          console.log("Successfull data1 : " + data.toString + " status :" + status);
        }).error(function(data, status, header, config) {
        console.log("Failed data1 : " + data + " status :" + status);

      });
    }
    getDeviceFromDb();
  }

})();

错误是

   Error: $http is not a function
AddController/getDeviceFromDb@http://localhost/fcmDashBoard     /add/add.controller.js:113:19
 AddController@http://localhost/fcmDashBoard/add/add.controller.js:129:3
 invoke@https://code.angularjs.org/1.2.20/angular.js:3918:14
 instantiate@https://code.angularjs.org/1.2.20/angular.js:3929:23
$ControllerProvider/this.$get</<@https://code.angularjs.org/1.2.20    /angular.js:7216:18
 nodeLinkFn/<@https://code.angularjs.org/1.2.20/angular.js:6607:34
 forEach@https://code.angularjs.org/1.2.20/angular.js:332:11
nodeLinkFn@https://code.angularjs.org/1.2.20/angular.js:6594:11
compositeLinkFn@https://code.angularjs.org/1.2.20/angular.js:6039:13
publicLinkFn@https://code.angularjs.org/1.2.20/angular.js:5934:30
ngViewFillContentFactory/<.link@https://code.angularjs.org/1.2.20/angular-    route.js:921:7
nodeLinkFn@https://code.angularjs.org/1.2.20/angular.js:6648:13
compositeLinkFn@https://code.angularjs.org/1.2.20/angular.js:6039:13
publicLinkFn@https://code.angularjs.org/1.2.20/angular.js:5934:30
createBoundTranscludeFn/boundTranscludeFn@https://code.angularjs.org /1.2.20/angular.js:6059:21
controllersBoundTransclude@https://code.angularjs.org/1.2.20/angular.js:6669:18
update@https://code.angularjs.org/1.2.20/angular-route.js:871:25
$RootScopeProvider/this.$get</Scope.prototype.$broadcast@https://code.angularjs.org/1.2.20/angular.js:12887:15
updateRoute/<@https://code.angularjs.org/1.2.20/angular-route.js:552:15
qFactory/defer/deferred.promise.then/wrappedCallback@https: //code.angularjs.org/1.2.20/angular.js:11498:31
qFactory/defer/deferred.promise.then/wrappedCallback@https://code.angularjs.org/1.2.20/angular.js:11498:31
qFactory/ref/<.then/<@https://code.angularjs.org/1.2.20/angular.js:11584:26
$RootScopeProvider/this.$get</Scope.prototype.$eval@https://code.angularjs.org/1.2.20/angular.js:12608:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@https://code.angularjs.org/1.2.20/angular.js:12420:15
$RootScopeProvider/this.$get</Scope.prototype.$apply@https://code.angularjs.org/1.2.20/angular.js:12712:13
done@https://code.angularjs.org/1.2.20/angular.js:8315:34
completeRequest@https://code.angularjs.org/1.2.20/angular.js:8527:7
createHttpBackend/</xhr.onreadystatechange@https://code.angularjs.org/1.2.20/angular.js:8466:1
 <div class="ng-scope" ng-view="">`

当我将链接直接放入浏览器时,我得到了结果。但我无法从我的Angularjs中解析它。

请帮我找到解决方案

2 个答案:

答案 0 :(得分:3)

删除函数中的$ http注入: 之前:

 var getDeviceFromDb = function(data,$http) {

之后:

 var getDeviceFromDb = function(data) {

原因是$ http已经注入你的控制器,可用于该控制器内的所有功能。

答案 1 :(得分:0)

你的功能在控制器之外给控制器内部。试试这段代码。

  (function() {
  'use strict';
  var app = angular.module('app');
  app.controller('AddController', AddController);
  function AddController($scope, $http) {
    $scope.mArray = [];
    $scope.clickButton = function() {
      $scope.conn = {
        'deviceToken': '',
        'deviceOs': '',
        'userName': ''
      };
      if ($scope.device_token == "" || $scope.device_os == "" || $scope.user_name == "" || $scope.device_token == undefined || $scope.device_os == undefined || $scope.user_name == undefined) {
        alert("Please fill the fields");
      } else {
        $scope.conn.deviceToken = $scope.device_token;
        $scope.conn.deviceOs = $scope.device_os;
        $scope.conn.userName = $scope.user_name;
        $scope.mArray.push($scope.conn);
        $scope.addDeviceToDb($scope.conn);
        console.log("hai -" + $scope.mArray.length);
        for (var i = 0; i < $scope.mArray.length; i++) {
          console.log($scope.mArray.indexOf($scope.mArray[i]) + 1);
        }
      }        

    $scope.PushNotification = function(row) {
      // use $.param jQuery function to serialize data from JSON
      console.log(row);
      var data = JSON.stringify({
        "notification": {
          "title": "Ionic 2 FCM Starter", // Any value
          "body": "new Message", // Any
          // value
          "sound": "default", // If you want notification sound
          "click_action": "FCM_PLUGIN_ACTIVITY", // Must be present
          // for Android
          "icon": "fcm_push_icon" // White icon Android resource
        },
        "data": {
          "param1": "value1"
        },
        "to": row.deviceToken,
        // "cYK3qwfbb54:APA91bGx0kMMvzyKudcjObxZO5slHg5b8PB8wysfdMf_1wlfErkM7XO7sMHj-tL_a9hDkZ-gyPXzr6wm1NWDNIpfcl5xN1vCgESB4HeUyG4pHpjfCeXcKY7th1GRJbMC84zTJAt3fXKT",
        // Topic or single device
        "priority": "high", // If not set, notification won't be
        // delivered on completely closed iOS
        // app
        "restricted_package_name": "" // Optional. Set for application
          // filtering
      });

      var config = {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'key=AAAAgXKnYTY:APA91bFRvdDdqD2vnY3kFQjOAu1lgYVY7DgsxsA9d3p2WOvQp1txbEqnibBDRjfv5duq1SG0NdYZeUNXzg_fI8d4p--9O-po2-ISDwlg4qh3uQdCprLQV5n8thJaIWXOz3eHYSbIdB33FC7x_EjaACBQLb44HflgBQ'
        }
      }

      $http.post('https://fcm.googleapis.com/fcm/send', data, config)
        .success(function(data, status, headers, config) {
          $scope.PostDataResponse = data;
        }).error(
          function(data, status, header, config) {
            $scope.ResponseDetails = "Data: " + data + "<hr />status: " + status + "<hr />headers: " + header + "<hr />config: " + config;
          });
      alert("Message Successfully send");
    }

    $scope.addDeviceToDb = function(data) {

      $http({
        method: 'POST',
        url: "http://10.10.14.54:8080/FcmBackend_ws/rest/add_device",
        headers: {
          'Content-Type': 'application/json'
        },
        transformRequest: function(obj) {
          var str = [];
          for (var p in obj)
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
          return str.join("&");
        },
        data: {
          token: data.deviceToken,
          device_os: data.deviceOs,
          device_version: data.userName
        }
      }).success(
        function(data, status, headers, config) {
          console.log("Successfull data : " + data.toString + " status :" + status);
        }).error(function(data, status, header, config) {
        console.log("Failed data : " + data + " status :" + status);

      });
    }
    var getDeviceFromDb = function(data) {
      var response = $http({
        method: 'GET',
        url: "http://localhost:8080/FcmBackend_ws/rest/devices",
        withCredentials: true,
        headers: {
          'Content-Type': 'application/json; charset=utf-8'
        }
      }).success(
        function(data, status, headers, config) {
          console.log("Successfull data1 : " + data.toString + " status :" + status);
        }).error(function(data, status, header, config) {
        console.log("Failed data1 : " + data + " status :" + status);

      });
    }
    getDeviceFromDb();
  }
}
})();