Skip to content

[Form] invalid CSRF token in ajax calls in production mode #9422

Closed
@FredoVelcro-zz

Description

@FredoVelcro-zz

Hello,

I am trying to submit my form via ajax (using jQuery latest version) like this :

var $form = $("form#search");
var values = $form.serialize();
jqhr = $.post(
                    $form.attr("action"),
                    values,
                    function(data) {
                        console.log(data);
                    },
                    "json"
                );

in dev mode, i received my data, it is OK.
But in production mode i have an "invalid token error " :

Le jeton CSRF est invalide. Veuillez renvoyer le formulaire

My controller :

public function ajaxFormSubmitAction(Request $request)
    {
        if (!$request->isXmlHttpRequest())
            throw new \HttpRequestMethodException();

        $form = $this->createForm(new SearchType(), new Search();));

        if ($request->isMethod('POST'))
        {

            $form->submit($request);

            if ($form->isValid())
            {                
                return New JsonResponse(array(  "status"    => true,
                                                "count"     => 123));
            }
// => reached in production mode but not in dev mode !!!
            print_r($form->getErrors());
        }

        return New JsonResponse(array(  "status"    => false,
                                        "count"     => 0));
    }

I don't understand why CSRF validation is OK in DEV mode and not in PRODUCTION mode ...! strange !!??

Sorry for posting this issue here but i did'nt find any other solution on forums etc. so wondering if it is a Symfony2 Form / CRSF issue...

Note : using Symfony2.4 beta2 / PHP 5.5.5 / Apache 2.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions