11

i really don't understand how to handle with post data from ajax request. This is my javascript:

$.ajax({
     type: "POST",
     url: Routing.generate('save'),
     contentType: 'application/json; charset=UTF-8',
     data: {
          title: title,                
          description: description,
          questions: questions,              
         }
  });

The only way to get the data inside my controller action is this:

$content = $request->getContent()

$content is a url parameter string. Why don't i get the data normally with:

$request->get('title')

What is the correct way to handle the post data with jquery ajax methd?

Thank you very much.

EDIT

So, i found out the following issue:

In my current project the request looks like this:

https://dl.dropboxusercontent.com/u/17861060/false.png

$.ajax({
            type: "POST",
            url: Routing.generate('poll_save'),                
            data: {
                title: title                    
            }
        })

The data is requested via Request Payload but i don't know why.

In a clean project the request looks like this:

https://dl.dropboxusercontent.com/u/17861060/right.png

$.ajax({
                type: "POST",
                url: '{{path('_demo')}}',                    
                data: {
                    title: 'title',                
                    description: 'description',
                    questions: 'questions',
                    pollid: 1                        
                }
            })

Anything in my project is going wrong. Do you have an idea why the data is requested via Request Payload?

4 Answers 4

9

Do you use the request object in your controller?

<?php
namespace Acme\DemoBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
//...other things to use

class MyController extends Controller
{
    public function handleRequestAction() {

        $request = $this->get('request');
        //request your data
        $title   = $request->get('title');
        //or in one line
        $title   = $this->get('request')->request->get('title');
    }
}
?>

This is my normal way when I want to get data from an ajax call. Could you post what $content contains?

I see no problem with posting the data like you did. Constructing a json object might be helpful but the way you're doing it seems fine to me. I did this too.

EDIT

Normally you could also access all data in the request by doing this:

$all = $request->request->all();

Maybe you could then var_dump() the variables to see if something is in them.

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

10 Comments

content was a string like this: title=sadfsdf&description=&questions%5B357147573695183%5D%5Bquestion%5D=sdfas&questions%5B357147573695183%5D%5Bdescription%5D=&questions%5B357147573695183%5D%5Boptional%5D=false&questions%5B357147573695183%5D%5Bqtype%5D=1&questions%5B357147573695183%5D%5Bsort%5D=1&questions%5B357147573695183%5D%5Bid%5D=357147573695183&questions%5B357147573695183%5D%5Binput%5D%5B%5D=fsdfasdf&questions%5B357147573695183%5D%5Binput%5D%5B%5D=safsdf&questions%5B357147573695183%5D%5Bvalid%5D=true&questions%5B357147573695183%5D%5Badd%5D=true&pollid=
$request->request->all(); was also empty. only getContent() gaves me any data. when i add the data to the url part, everything is fine. but i need to send the data via the data parameter because the post data can be very large.
how does your ajax method look like?
Like yours, but I'm not using the bundle to generate routes (forgot the name of the bundle). Perhaps the problem is that you define a contentType. Did you try to leave it out and see what happens? jQuery Docu says "Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases"
only getContent() has content, the rest is empty. I left contentType alos empty, it was the same result.
|
1

You can construct your json object and pass the JSON object to your controller using JSON.stringify.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify

var obj = {
      title: title,                
      description: description,
      questions: questions              
};

$.ajax({
 type: "POST",
 url: Routing.generate('save'),
 contentType: 'application/json; charset=UTF-8',
 data: JSON.stringify(obj)
});

3 Comments

And this is the normal workflow? I thought the ajax mehtod will handle it alone?
Yes, because $.ajax does not serialize the data to JSON.
But why can't i use the normal requets methods? I only get the data via $request->getContent()
1

quiz - form name serialize -populate the variables

 $.ajax({
            url: $("#quiz").attr("action"),
            data: $("#quiz").serialize(),
            type: 'POST'
 });

or

$.ajax({
                url: $("#commentForm").attr("action"),
                data: {
                    comment: commentFormID.val()
                },
                type: 'POST'
});

Controller - More like what previous comments suggested.

$request = $this->get('request');
$usercomment=$request->request->get('parameterName');

Comments

0

Why Json? I meant is a requirement to content type json? if not, this is the way I handle ajax and using FOSRoutingbundle that I can see you are using.

$(document).ready(function(){
    $('#myForm').submit( function(e){       

        e.preventDefault();
        var $form = $(this);
        var $formPHP = $form.serializeArray();
        var $url = Routing.generate( 'route_to_use');

        $.post( $url, $formPHP, function(data){
        .....
        });

    });    
});

Then in the controller you can use as a normal request.

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.