2017-01-18 6 views
0

Ich erhalte diese Störung, die ich das Protokoll während Json ParsenAngular Js-Json Parsing Fehler

(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(); 
    } 

})(); 

Und der Fehler

Error: $http is not a function 
AddController/[email protected]://localhost/fcmDashBoard  /add/add.controller.js:113:19 
[email protected]://localhost/fcmDashBoard/add/add.controller.js:129:3 
[email protected]://code.angularjs.org/1.2.20/angular.js:3918:14 
[email protected]://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 
[email protected]://code.angularjs.org/1.2.20/angular.js:332:11 
[email protected]://code.angularjs.org/1.2.20/angular.js:6594:11 
[email protected]://code.angularjs.org/1.2.20/angular.js:6039:13 
[email protected]://code.angularjs.org/1.2.20/angular.js:5934:30 
ngViewFillContentFactory/<[email protected]://code.angularjs.org/1.2.20/angular- route.js:921:7 
[email protected]://code.angularjs.org/1.2.20/angular.js:6648:13 
[email protected]://code.angularjs.org/1.2.20/angular.js:6039:13 
[email protected]://code.angularjs.org/1.2.20/angular.js:5934:30 
createBoundTranscludeFn/[email protected]://code.angularjs.org /1.2.20/angular.js:6059:21 
[email protected]://code.angularjs.org/1.2.20/angular.js:6669:18 
[email protected]://code.angularjs.org/1.2.20/angular-route.js:871:25 
$RootScopeProvider/this.$get</[email protected]://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/[email protected]: //code.angularjs.org/1.2.20/angular.js:11498:31 
qFactory/defer/deferred.promise.then/[email protected]://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</[email protected]://code.angularjs.org/1.2.20/angular.js:12608:16 
$RootScopeProvider/this.$get</[email protected]://code.angularjs.org/1.2.20/angular.js:12420:15 
$RootScopeProvider/this.$get</[email protected]://code.angularjs.org/1.2.20/angular.js:12712:13 
[email protected]://code.angularjs.org/1.2.20/angular.js:8315:34 
[email protected]://code.angularjs.org/1.2.20/angular.js:8527:7 
createHttpBackend/</[email protected]://code.angularjs.org/1.2.20/angular.js:8466:1 
<div class="ng-scope" ng-view="">` 

Während ich den Link direkt in meinem Browser bin Putting Ich bin das Ergebnis bekommen. Aber ich kann es nicht von meinen Angularjs analysieren.

Bitte helfen Sie mir die Lösung

Antwort

3

Entfernen Sie die $ http Injektion in der Funktion zu finden: Vorher:

var getDeviceFromDb = function(data,$http) { 

Nach: wird

var getDeviceFromDb = function(data) { 

Der Grund ist der $ http bereits in Ihren Controller eingespeist, der in allen Funktionen dieses Controllers verwendet werden kann.

+0

Mit dem versucht. Jetzt bekommen \t "Fehler data1: status: 0" –

+0

Also ich denke, das wäre ein Problem mit Ihrer API, die Sie aufrufen und kein Winkelfehler. –

+0

Aber ich bekomme Ergebnis in Web-Browser –

0

Ihre Funktionen sind außerhalb des Controllers innerhalb des Controllers geben. Probieren Sie diesen Code aus.

(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(); 
    } 
} 
})(); 
+0

Die Funktion wird aufgerufen, dass, warum ich Log "Failed data1: status: 0" –

+0

kann Sie plunkr hinzufügen. –