静止角度开机自检失败 https:// 选项通过

Restangular POST fails https:// OPTIONS pass

本文关键字:选项 https 开机自检 失败 静止      更新时间:2023-09-26

我能够将大部分现有服务转换为使用Restangular。除了POST之外,一切都在正常工作。

有效的原始POST服务

app.service('APIService', function($http, $q){
...
this.post = function(api, route, params){
        var d = $q.defer();
        $http({
            url : base_urls[api] + route,
            method : 'POST',
            data : params,
            withCredentials: true,
            useXDomain : true,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).success(function(data){
            d.resolve(data);
            try{
               toastr.success(toastMsg[route].win, 'Success');  
            } catch(err){}
        }).error(function(data){
            d.reject(data);
            try{
                toastr.error(toastMsg[route].fail, 'Whoops');
            } catch(err){}
        });
        return d.promise;
    }
});

这是这样用的:

app.controller('AuthController', function($scope, APIService){
    $scope.login = function(username_or_email, password, redirectUrl){
        APIService.post('root', '/login', {
            'username_or_email' : username_or_email, 
            'password' : password
        }).then(function(r){
        if(r.success){
            window.location = redirectUrl;
        }else
        {
          // handle this 
        }
      });
   };
});

转换为 Restangular

app.controller('AuthController', function ($scope, toastrFactory, Restangular) {
    $scope.login = function (username_or_email, password, redirectUrl) {
        var login = Restangular.one('auth'),
            creds = {
                'username_or_email': username_or_email,
                'password': password
            };

        login.post('login', creds).then(function (r) {
            window.location = redirectUrl || '/profile';
        }, function () {
            toastrFactory.error(['Error', 'Login not successful'])
        })
    };
});

以上未通过飞行前OPTIONS通过,并放弃。我的原始服务和我尝试使用的 Restangular 调用有什么区别?

值得注意的是,我确实为 Restangular 设置了默认配置参数(以镜像原始服务)

RestangularProvider.setBaseUrl('https://dev.foo.com/');
    RestangularProvider.setDefaultHttpFields({
        withCredentials: true,
            useXDomain : true,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
    });

奇怪的是我的 Restangular GET需要 https://WORK的凭据,并成功通过OPTIONS阶段,并设法发送cookie数据。

任何帮助,不胜感激。谢谢。

我不确定您要到达的实际路线,但Restangular.one('auth')似乎您需要定义资源标识符(例如。 POST /auth/123?username_or_email=moi)。

如果您尝试访问POST /auth?username_or_email=moi(在 HTTP 参数中使用cred),请尝试Restangular.all('auth')

如果这不能解决问题,请提供您在浏览器的网络检查器中看到的 URI 以及您想要访问的 URI。