JMS Serializer无法返回响应

时间:2015-12-09 23:57:10

标签: angularjs symfony jmsserializerbundle

我最近加入了JMS Serializer包来处理数组并将其转换为JSON

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

public function searchcarAction(Request $request) 
{
    $em = $this->getDoctrine()->getManager();

   // $car = array();
    //$cars = $em->getRepository('VehicleBundle:Car')->findAll();
   // foreach ($cars as $cart) {
       // $car[] = array('id'=>$cart->getId(),'name' => $cart->getName());
   // }

    $cars = array('name' => 'john','middlename'=> 'gon');

    $serializer = $this->container->get('jms_serializer');

    $jsonGroup= $serializer->serialize($cars,'json');

    return $jsonGroup;
}
public function carAction()
{
    return $this->render('VehicleBundle:Car:car_search.html.twig');
}

car.yml

search_car:
   path: /car-search
   defaults: { _controller: "VehicleBundle:Car:searchcar" }

car_action:
  path: /search
  defaults: { _controller: "VehicleBundle:Car:car" }

JMS Serialiser在AppKernel.php中注册

这将返回500错误

  

控制器必须返回一个回复({"名称":" john","中间名":" gon"})。

  if (null === $response) {
     $msg .=' Did you forget to add a return statement somewhere in your   controller?';
  }
   throw new \LogicException($msg);
  }
  }
  return $this->filterResponse($response, $request, $type);

我的控制器出了什么问题?

模板

car_search.html.twig

 {% extends '::base.html.twig' %}

 {% block body %}

 <div ng-app="myApp" ng-controller="customersCtrl">
 Search your Groups Here: <input type="text" ng-model="searchMe.name"/>
 <input type="checkbox" ng-model="exactMatch"/>Exact Match<br/>
 <div class="[ form-group ]">
    <input type="checkbox" ng-model="click" name="fancy-checkbox-default" id="fancy-checkbox-default" autocomplete="off" />
    <div class="[ btn-group ]">
        <label for="fancy-checkbox-default" class="[ btn btn-default ]">
            <span class="[ glyphicon glyphicon-ok ]"></span>
            <span> </span>
        </label>
        <label for="fancy-checkbox-default" class="[ btn btn-default active ]">
            Hide 
        </label>
    </div>
</div>
<table id="table" class="table" ng-hide="click">
    <thead>
        <tr>
            <th>Name</th>
            <th>Model</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="x in names | filter:searchMe:exactMatch | limitTo: 2">
            <td>//x.name//</td> 
            <td>//x.model//</td>
        </tr>
    </tbody> 
</table>
</div>  
{% endblock %}
{% block javascripts %}
    {#{ parent() }#}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="//code.angularjs.org/1.4.8/angular.js"></script>
    <script>
        var app = angular.module('myApp', []);

        app.config(function($interpolateProvider) {
        $interpolateProvider.startSymbol('//');
        $interpolateProvider.endSymbol('//');
        });

        app.controller('customersCtrl',['$scope','$http',function($scope, $http) {
            //$http.get("http://localhost:8093/voters/voters_angular")
            $http.get("{{ path('search_car') }}")
            .success(function (response) {
                $scope.names= JSON.parse(response);
            });
        }]);
        //console.log(names);
    </script>
{% endblock %}

JMS序列化程序在这里不起作用吗?

1 个答案:

答案 0 :(得分:0)

您正在从控制器返回一个字符串而不是Response对象。

而不是:

return $jsonGroup;

您应该返回响应,例如:

return new \Symfony\Component\HttpFoundation\Response($jsonGroup, 200, ['Content-Type' => 'application/json']);