【问题标题】:Cross-Origin Request Blocked in IonicIonic 中的跨域请求被阻止
【发布时间】:2015-08-14 17:37:02
【问题描述】:

你好朋友,我正在使用 ionic 框架开发 android 应用程序。在服务端,我使用 Symfony 框架进行 Web 服务。我使用 Web 服务成功获取数据。但是当我发布数据时,我收到错误 Cross-Origin Request Blocked .这是我的代码:

.controller('login', function($scope,$state,Stack,$ionicPopup,$ionicLoading,$cordovaLocalNotification, $ionicPlatform) {




$scope.user = {};


      var username = $scope.user.username;
      var password = $scope.user.password;

       Stack.Login(username,password).then(function(response){

        var login = response.data;


        });  
  }

  $state.go('login');

})

服务是这样的:

Login:function(username,password){
        //alert(info);
           var Url = baseurl+'login';

           var defer = $q.defer();
           alert(username);
          // console.log(item);
           $http.post(Url,username).
              success(function (data, status, headers, config) {

                  defer.resolve(data);
              }).
              error(function (data, status, headers, config) {
                  defer.reject();
              });

            return defer.promise;
   },

在 Symfony 中:

public function loginAction()
         {
$response = new Response(json_encode(array('data' =>'Faliure')));
            $response->headers->set('Access-Control-Allow-Origin', '*');
            $response->headers->set('Content-Type', 'application/json');
            return $response;
}

【问题讨论】:

    标签: php android angularjs symfony ionic-framework


    【解决方案1】:

    经过大量搜索,我终于找到了 Cross-Origin Request Blocked 的解决方案。其实问题出在服务器端。

    解决方案是安装这个:

    php composer.phar require nelmio/cors-bundle:~1.0
    

    然后添加AppKernal.php:

    new Nelmio\CorsBundle\NelmioCorsBundle(),
    

    更新 Config.yml 文件:

    nelmio_cors:
       defaults:
           allow_credentials: true
           allow_origin: []
           allow_headers: []
           allow_methods: []
           expose_headers: []
           max_age: 0
           hosts: []
       paths:
        '^/':
            allow_origin: ['*']
            allow_headers: ['origin', 'content-type']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE','OPTIONS']
            max_age: 3600
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2015-02-08
      • 2021-06-26
      • 2014-10-13
      • 2019-11-04
      • 2017-02-06
      • 2017-01-04
      相关资源
      最近更新 更多