2015-04-26 26 views
6

我有一個彈出窗口調用RESTful後端來執行Oauth身份驗證,但是當返回結果時,它會在彈出窗口中顯示JSON而不是關閉彈出窗口並將JSON存儲在一個模型。我該如何解決?從彈出窗口獲取JSON結果。 Angular.JS

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

爲什麼你需要擺在首位額外的窗口?如果它在您的域上,您可以使用postMessage API將數據傳回主窗口 – charlietfl

+0

當我嘗試時,根本沒有任何反應。 – Knoland

+0

當你嘗試什麼? – charlietfl

回答

0

上述情況可能不是解決Angular問題的最佳方法。我假設這是寫在一個控制器裏面。如果是這樣,撥打後端服務最好使用$http服務。要做到這一點,

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