문제

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 }
도움이 되었습니까?

해결책

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.

다른 팁

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top