Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JSON Result from popup window. Angular.JS

I've got a pop-up window that calls a RESTful backend to do Oauth authentication, but when the result is returned, it displays the JSON in the popup window rather than closing the popup window and storing the JSON in a model. How do I fix this?

this.socialLogin = function(provider) {
  var url = urlBase + '/' + provider,
      width = 1000,
      height = 650,
      top = (window.outerHeight - height) / 2,
      left = (window.outerWidth - width) / 2,
      socialPopup = null;

  $window.open(url, 'Social Login', 'width=' + width + ',height=' + height + ',scrollbars=0,top=' + top + ',left=' + left);
};
like image 836
Knoland Avatar asked Apr 26 '15 00:04

Knoland


1 Answers

The situation described is likely not the best way to solve your problem in Angular. I am assuming that this is written inside of a controller. If so, making a call to a backend service is best done with the $http service. To do so,

controller.js

angular.module('example')
  .controller(['$scope', '$http', '$window', function($scope, $http, $window) {

  $scope.socialLogin = function(urlEnd) {
    $http.get(urlBase + '/' + urlEnd) // Should probably be posting here...
      .then(function(res) { // Your JSON response here
        $scope.doSomethingWithTheResponse(res.data);
      }, function(err) { // Handle your error
        $window.alert("We got us an error!\n" + JSON.stringify(err));
      });
    }
  }])
like image 83
Kris Molinari Avatar answered Nov 17 '22 10:11

Kris Molinari