nboulfroy
nboulfroy

Reputation: 221

Symfony 3.4 - jQuery AJAX request no result found in controller

Currently, I work on a project and I try to get data from an AJAX request which is the method is "POST".

This is my JS code :

function ajax() {
    var form = new FormData();
    // Data from contenteditable which is id is "title".
    form.append('article[title]', $('#title').html().trim());
    // Data from contenteditable which is id is "content".
    form.append('article[content]', $('#content').html().trim());
    // Data from input checkbox which is id is "article_parution".
    form.append('article[parution]', $('#article_parution').val());
    // Data from input hiden which is id is "article__token".
    form.append('article[_token]', $('#article__token').val());

    $.ajax({
        method: "POST",
        url: '{{ path("add_article") }}', // /admin/add
        processData: false,
        cache: false,
        data: form
    })
}

I have used "console.log()" to verify is the data exists and it's the case.

The problem is when I try to use this in my controller :

$title = $request->request->get('article[title]');

$title is null, why ?

Moreover, this URL (/admin/add) is in a protected area by FosUserBundle in the website (the user must be connected in admin to access in this website part).

Upvotes: 0

Views: 731

Answers (1)

Thomas Bredillet
Thomas Bredillet

Reputation: 331

You can dump your request for see what you must get

dump($request->request);

I think the solution is

$request->request->get('article')['title'];

You can also add '' in your article['title'] in Javascript

Upvotes: 1

Related Questions