Domanda

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:

enter image description here

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 }
È stato utile?

Soluzione

My guess is you defined the route with a trailing slash. If you do that, Symfony redirects the request to the path with a trailing slash. Try POSTing to the same URL but with a trailing slash.

To avoid this kind of problems, consider using FOSJsRoutingBundle. It allows you to use the same routes in JS you use in Symfony.

Altri suggerimenti

symfony replaces default PHP globals

try

$r = array('ok'=>$request->request->all());

http://symfony.com/doc/current/components/http_foundation/introduction.html

what i used to do with post request :

//in controller

$request = $this->get('request');
$data = $request->request->get('data');

//in routing.xml

<route id="ajax_save_contentsAction" pattern="/ajax_save_contents">
        <default key="_controller">Bundle:ajax_save_contents</default>
        <requirement key="_method">post</requirement>
</route>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top