3

I would like to know why this is not working , I have a AngularJS app witch sends trough AJAX data to a Symfony2 Application. As you can see, data is sent in my network console

<?php

namespace Supbox\CloudBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
class FolderController extends Controller
{
    public function createAction(){
        $post = $this->getRequest()->request;
        $name = $post->get("name");
        $folder = $post->get("folder");
        var_dump($post);
        die; 
    }
}

AngularJS code

    $http({
            method: 'POST', 
            url: route.folder.create, 
            data: {
                folder: $scope.id,
                name: name
            }
        })

Opera Network Console Output

Request URL:http://localhost/supbox/web/box/folder/create
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept-Encoding:gzip,deflate,lzma,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:25
Content-Type:application/json;charset=UTF-8
Host:localhost
Origin:http://localhost
Referer:http://localhost/supbox/web/box/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36 OPR/20.0.1387.82
Request Payloadview source
{folder:1, name:Ang}
Response Headersview source
Connection:Keep-Alive
Content-Length:431
Content-Type:text/html
Date:Mon, 24 Mar 2014 13:25:53 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.4 (Win64) OpenSSL/1.0.1d PHP/5.4.12
X-Powered-By:PHP/5.4.12
6
  • 3
    What does var_dump($post) display ? Commented Mar 24, 2014 at 13:43
  • 1
    could you show how you create the post on angular side? Commented Mar 24, 2014 at 13:47
  • $post is a instance of ParameterBag which is empty... img15.hostingpics.net/pics/883753Sanstitre.png Commented Mar 24, 2014 at 14:06
  • That means you are posting data through header, right? Commented Mar 24, 2014 at 14:10
  • i think so ... i use angular for posting with AJAX Commented Mar 24, 2014 at 14:14

3 Answers 3

4

If you (Angular JS) post data through header as JSON you need to change your code like this:

public function createAction(){
    $post = $this->getRequest()->getContent();
    $post = json_decode($post);
    $name = $post->name;
    $folder = $post->folder;
    var_dump($post);
    var_dump($name); // null
    var_dump($folder); // null
    die; 
}
Sign up to request clarification or add additional context in comments.

5 Comments

it works... fine ... but i didn't get why. Why do I have to do something like that in POST whereas it works without for Get. any explanation is welcome :)
Because Angular JS does not post your data in normal post; it changes them ot JSON then pass the post data through header content. It's the nature of Angular JS (just in case if this works mark it as green ;-))
ok, is there a way to change that behaviour ? (for angular using jQuery's POST way)
I think you can change it to normal if you use $http.post instead. Check this documentation [docs.angularjs.org/api/ng/service/$http]
Your link is corrupted, please remove brackets ;)
0

Dont know why, Angular $http sends data as request body, JSON encoded whereas Symfony2 is reading $_GET and $_POST arrays.

So you got 2 solutions:

1- Update Php code, you could override SF2 Request class (https://gist.github.com/ebuildy/fe1e708e466dc13dd736)

2- Update Js code, you can "transform" the $http request (https://gist.github.com/bennadel/11212050)

Comments

0

A bundle has been created to solve this problem, and it's very light.

qandidate-labs/symfony-json-request-transformer

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.