我有一个网站,我正在尝试使用 AngularJS Controller 通过 https 登录。每次我尝试登录时都会收到这样的消息
Mixed Content: The page at 'https://example.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://example.com/'. This request has been blocked; the content must be served over HTTPS."
该网站在 nginx 上运行,配置为将所有内容重定向到 https。
这是我的 Controller 代码。
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!");
});
}
});
我的后端在 Flask 上运行,该部分似乎一切正常。
目前它会像失败一样停止,但是当我重新加载主页时,它已经成功登录了。
当所有内容都是通过 https 加载时,为什么 AngularJS 将 XMLHttpRequest
推送到 http,以及如何修复它以使其正常工作?
最佳答案
我刚刚发现了我的问题。在我的后端 Flask 之前设置为在成功时自行进行重定向,这意味着它无法正确返回登录成功的 angularjs $http.post()
响应。一旦我在 python 中重写了返回值以用 json 响应
return jsonify({'success': False, 'msg': "用户名或密码无效"})
或
return jsonify({'success': True, 'msg': "Logged in success"})
AngularJS 请求正常工作。
关于AngularJS 在 POST 上从 https 请求 http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28465036/