Access-Control-Allow-Origin代码405?

时间:2016-11-13 17:41:42

标签: angularjs cordova ionic-framework jhipster satellizer

我正在使用Jhipster和Jhipster-ionic以及cordova开发移动应用程序。 目前,我正在使用基于令牌的AngularJS身份验证(Satellizer)登录OAuth 2.0,我遇到了CROSS原因的问题。

我在Satellizer-ionic上关注了这个例子,我做了很多事情

我的配置:

 .config(function($authProvider) {
   $authProvider.httpInterceptor = false;
   $authProvider.withCredentials = true;

    var commonConfig = {
      popupOptions: {
        location: 'yes',
        toolbar: 'yes',
        width: window.screen.width,
        height: window.screen.height
      }
    };

    if (ionic.Platform.isIOS() || ionic.Platform.isAndroid()) {
      commonConfig.redirectUri = 'http://localhost:3000/';
    }

    $authProvider.facebook(angular.extend({}, commonConfig, {
      clientId: 'MyFacebookId',
      url: 'http://localhost:8080/social/signup'
    }));

    $authProvider.google(angular.extend({}, commonConfig, {
      clientId: 'Myid.apps.googleusercontent.com',
      url: 'http://localhost:8080/social/signup'


      }));
      })
      .run(function($ionicPlatform) {
        $ionicPlatform.ready(function() {
          if (window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
          }
          if (window.StatusBar) {
            StatusBar.styleDefault();
          }
        });
      });

控制器:

vm.authenticate = function(provider) {
  $auth.authenticate(provider)
    .then(function(res) {
      $ionicPopup.alert({
        title: 'Success',
        content: 'You have successfully logged in!'
      })
      console.log("yes google login works");
      console.log('success', 'Welcome', 'Thanks for coming back, ' + res.user.displayName + '!');

    })
    .catch(function(error) {
      console.log(error);
      $ionicPopup.alert({
        title: 'Error',
        content: error.message || (error.data && error.data.message) || error
      });
      console.log("too bad" + error.data);
    });
};

HTML:

<button class="btn btn-full btn-fb active db" ng-click="vm.authenticate('google')" type="submit" translate="{{'welcome.loginGoogle' |translate}}"></button>

<button class="btn btn-full btn-fb active db" ng-click="vm.authenticate('facebook')" type="submit" translate="{{'welcome.loginFacebook' |translate}}"></button>

但我收到了这个错误:

enter image description here

我在application.yml上删除了cors

cors: #By default CORS are not enabled. Uncomment to enable.
        allowed-origins: "*"
        allowed-methods: GET, PUT, POST, DELETE, OPTIONS
        allowed-headers: "*"
        exposed-headers:
        allow-credentials: true
        max-age: 1800     

我的Jhipster版本是v3.5.1。

1 个答案:

答案 0 :(得分:0)

我在我的代码上做了一些错误,commonConfig.redirectUri应该是我的后端URL,这里是解决方案:

public function getAll() {
            $sql = $this->conn();

            $result = $sql->query("SELECT row_name FROM tab");

            if($result->num_rows >= 1) {
                while($row = $result->fetch_assoc()) {
                    $unfiltered[] = $row['row_name'];
                }
            }

            $filtered = array_map(array($this, 'clearDots'),$unfiltered);

            print json_encode($filtered);
        }

        private function clearDots($variable) {
            str_replace("."," ",$variable);
        }