0

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).

2
  • 1
    what you are getting in console Commented Jan 16, 2018 at 4:08
  • Nothing is in console.log() when I try to return value and $request object (Request $request), I have only "article[title]" and when I try to return only this with "$request->request->get('article[title]'), the result is null ... Commented Jan 16, 2018 at 7:57

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.