No post data with Ajax JQuery Json and Symfony 2 -
i'm trying send data in json format via jquery ajax, seems impossible receive posted data de controller's action.
here 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 symfony2 controller
public function ajax_save_contentsaction(request $request) { if ($request->isxmlhttprequest()) { $r = array('ok'=>$_post); return new jsonresponse($r); } return new response('this not ajax!', 400); }
this works fine, except because in controller don't have post data.
things i've tried:
- do call method <- works need post
- in controller had: $this->getrequest()->get('data'); <- gives null
the weird thing in firebug noticed watching console this:
it seems if first call right, redirected , loses post data in way.
i'm stuck!! :_____((((
edit: add 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 }
my guess defined route trailing slash. if that, symfony redirects request path trailing slash. try posting same url trailing slash.
to avoid kind of problems, consider using fosjsroutingbundle. allows use same routes in js use in symfony.
Comments
Post a Comment