Hello Friends I am developing android app using ionic framework.In the serve side I use Symfony framework for web services.I am successfully fetch data using web services.but when I post the data then I got error Cross-Origin Request Blocked.Here is My code:
.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');
})
Service is Like:
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;
},
In 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;
}
Finally after lot of search I found solution of Cross-Origin Request Blocked. Actually the problem is in the server side.
The Solution is Install this:
php composer.phar require nelmio/cors-bundle:~1.0
Then add in AppKernal.php:
new Nelmio\CorsBundle\NelmioCorsBundle(),
Update the Config.yml file:
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