i'm trying to send some data in json format via JQuery Ajax, but seems impossible to receive the posted data into de controller's action.
Here is the JQuery/Javascript code:
$.ajax({
type: "POST",
url: "app_dev.php/ajax_save_contents",
contentType: 'application/json',
data: {'data':'whatever'},
dataType: "json",
success: function(data)
{
alert(data.ok);
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert('Error : ' + errorThrown);
}
});
And this is the Symfony2 Controller
public function ajax_save_contentsAction(Request $request)
{
if ($request->isXMLHttpRequest()) {
$r = array('ok'=>$_POST);
return new JsonResponse($r);
}
return new Response('This is not ajax!', 400);
}
This works all fine, except because in the controller i don't have any post data.
Things that i've already tried:
- Do the call with method GET <- this works but i need do it by POST
- In the controller i had: $this->getRequest()->get('data'); <- this gives null
The weird thing in firebug that i noticed is that watching the console i get this:
It seems like if it does a first call right, but get redirected and then loses the post data in the way.
I'm stuck!! :_____((((
Edit: I add the security.yml:
security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
in_memory:
memory:
users:
user: { password: userpass, roles: [ 'ROLE_USER' ] }
admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/demo/secured/login$
security: false
secured_area:
pattern: ^/demo/secured/
form_login:
check_path: _security_check
login_path: _demo_login
logout:
path: _demo_logout
target: _demo
#anonymous: ~
#http_basic:
# realm: "Secured Demo Area"
access_control:
#- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }