AngularJS在POST上从https请求http

时间:2015-02-11 21:40:31

标签: angularjs nginx https flask xmlhttprequest

我有一个网站,我试图通过AngularJS控制器登录https。每次我尝试登录时都会收到这样的消息

  

混合内容:' 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!");
    });
}
});

我的后端正在Flask上运行,一切似乎都在该部分正常运行。

目前它会失败,但是当我重新加载主页时,它让我成功登录。

为什么AngularJS会在通过https加载所有内容时将XMLHttpRequest推送到http,如何修复它以使其正常工作?

1 个答案:

答案 0 :(得分:0)

我刚刚发现了我的问题。在我的后端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请求正常工作。

相关问题