2017-03-01 5 views
1

私はプラグインhttps://github.com/phonegap-build/PushPlugin/をAngular 1.3で使用しています。登録イベントを受信すると、regidをサーバーに送信する必要があります。 問題は、このコンテキストでサーバーを呼び出す$ httpオブジェクトがないことです。どうすればいいですか?

function onNotification(e){ 
    if(e.event == "registered"){ 
     var req = { 
      method: "POST", 
      url: "http://myurl.com/?var="+e.regid 
     }; 
     $http(req).success(function(data){ 
      alert(data); 
     }); 
    } 
} 
+0

あなたは 'onNotification'機能の親一部を投稿することができますか? –

答えて

0

変更$http呼び出しを次のように、.successが廃止されました。

$http({ 
    method: "POST", 
    url: "http://myurl.com/?var="+e.regid 
}).then(function successCallback(response) { 
    // this callback will be called asynchronously 
    // when the response is available 
    alert(response); 
}, function errorCallback(response) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
}); 

Ref。 :https://docs.angularjs.org/api/ng/service/ $ http

よろしく。

+0

$ HTTPを呼び出すと、onNotificationメソッドがコントローラ、サービス、ファクトリの内部にないため、 "$ http is undefined"というエラーが発生します。 –

1

私はちょうどイベントメソッドに$ HTTPを注入する方法を学びました:

$http = angular.injector(["ng"]).get("$http"); 
関連する問題