Javascript 从弹出窗口获取JSON结果。Angular.JS
我有一个弹出窗口,它调用RESTful后端来执行Oauth身份验证,但是当返回结果时,它会在弹出窗口中显示JSON,而不是关闭弹出窗口并将JSON存储在模型中。我该如何解决这个问题Javascript 从弹出窗口获取JSON结果。Angular.JS,javascript,json,angularjs,Javascript,Json,Angularjs,我有一个弹出窗口,它调用RESTful后端来执行Oauth身份验证,但是当返回结果时,它会在弹出窗口中显示JSON,而不是关闭弹出窗口并将JSON存储在模型中。我该如何解决这个问题 this.socialLogin = function(provider) { var url = urlBase + '/' + provider, width = 1000, height = 650, top = (window.outerHeight - height)
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);
};
所描述的情况可能不是解决问题的最佳方法。我假设这是写在控制器内部的。如果是这样,最好使用
$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));
});
}
}])
为什么你首先需要额外的窗口?如果它在您的域中,您可以使用PostMessageAPI将数据传递回主窗口。当我尝试这样做时,什么也不会发生;问题是后端正在从Twitter或FaceBook重定向到社交登录页面。您是否检查了服务器中oauth的后端实现。我以前用颌骨做过这种事。它所做的是,一旦oauth成功,它通常会请求回调url。现在,它调用success的url必须返回一个脚本,告诉它关闭窗口。在我们的案例中就是这样实施的。您可以试一试,它是一个RESTAPI,所以它只返回json。bcallback进入passport,并返回用户数据的json。窗口永远不会关闭,而只是显示原始json。