AngularJS在POST上向https请求http
问题描述:
我有一个网站,我试图通过https与AngularJS控制器进行登录。每次我试图登录我得到这样AngularJS在POST上向https请求http
混合内容的消息:在“https://example.com/”加载页面通过HTTPS,但要求一个不安全的XMLHttpRequest端点“http://example.com/”。此请求已被阻止;内容必须通过HTTPS提供服务。”
该网站上nginx的运行配置的一切重定向到https。
这里是我的控制器代码。
app.controller("LoginCtrl", function ($scope, $http, $window) {
$scope.formData = {};
$scope.remember_me = false;
$scope.doLogin = function(){
var xsrf = $scope.formData;
$http({
url: "/login",
method: "POST",
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
transformRequest: function(obj) {
var str = [];
for(var p in obj)
if (obj.hasOwnProperty(p)){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
},
data: xsrf
}).success(function(data) {
if($window.location.pathname == '/login') {
$window.location.href = '/';
}
else{
$window.location.reload();
}
});
}
$scope.doJsonLogin = function(){
var xsrf = $scope.formData;
$http.post('/', { 'u': $scope.formData.username, 'p': $scope.formData.password, 'c': $scope.remember_me }).success(function(data) {
if($window.location.pathname == '/login') {
$window.location.href = '/';
}
else{
$window.location.reload();
}
}).error(function (data, status, headers, config) {
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
alert("failed!");
});
}
});
我的后端上运行烧瓶,一切似乎都在正常工作。
目前它会失速,但当我重新加载主页,它让我成功登录。
为什么AngularJS会在通过https加载所有内容时将XMLHttpRequest
推送到http,以及如何修复它以使其正常工作?
答
我只是想出了我的问题。在我的后端中,Flask之前已设置为在成功时执行重定向,这意味着它没有正确地返回对登录成功的angularjs $http.post()
的响应。一旦我重写了我在python中的返回来响应json return jsonify({'success': False, 'msg': "Username or Password is invalid"})
或 return jsonify({'success': True, 'msg': "Logged in successfully"})
angularjs请求正常工作。
您是否在初始登录时在请求中看到任何与https相关的标头? – mindparse 2015-02-11 22:47:37
请求URL和所有临时标题显示'https'我不确定如何捕获来自angularjs的整个请求。 – 2015-02-12 14:49:06
这个问题可以解释一下:[MixedContent当我通过ajax加载https页面,但浏览器仍然认为它是http](http://stackoverflow.com/questions/28197528/mixedcontent-when-im-loading-https-page - 通过Ajax的,但是,浏览器仍然认为) – Danil 2016-01-12 20:47:29