【问题标题】:Restangular POST fails https:// OPTIONS passRestangular POST 失败 https:// OPTIONS pass
【发布时间】:2013-10-08 01:40:46
【问题描述】:

我能够将我现有的大部分服务转换为使用 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 GETs 需要 https://WORK 上的凭据,并成功通过 OPTIONS 阶段,并设法发送 cookie 数据。

感谢任何帮助。谢谢。

【问题讨论】:

    标签: javascript angularjs ssl https restangular


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 2021-06-30
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      相关资源
      最近更新 更多