Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularsJS POST JSON data to Symfony2

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
like image 317
Fabien Papet Avatar asked Jun 30 '26 18:06

Fabien Papet


1 Answers

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; 
}
like image 192
Javad Avatar answered Jul 03 '26 17:07

Javad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!