php - 如何在symfony2 con中发送json响应

我正在使用jQuery编辑我在Symfony中构建的表单。

我在jQuery对话框中显示该表单,然后提交它。

数据在数据库中正确输入。

但我不知道是否需要将一些JSON发送回jQuery。 其实我对JSON的事情有点困惑。

假设我在我的表中添加了一行jQuery,当我提交表单时,然后在提交数据后我想发送回那些行数据,这样我就可以动态添加表行来显示添加的数据。

我很困惑如何才能获得这些数据

这是我目前的代码

$editForm = $this->createForm(new StepsType(), $entity);

$request = $this->getRequest();

$editForm->bindRequest($request);

if ($editForm->isValid()) {
    $em->persist($entity);
    $em->flush();

    return $this->render('::success.html.twig');               
}

这只是带有成功消息的模板

user825904 asked 2019-09-10T10:23:14Z
5个解决方案
181 votes

Symfony 2.1

$response = new Response(json_encode(array('name' => $name)));
$response->headers->set('Content-Type', 'application/json');

return $response;

Symfony 2.2及更高版本

你有特殊的JsonResponse类,它将数组序列化为JSON:

return new JsonResponse(array('name' => $name));

但如果您的问题是如何序列化实体,那么您应该看看JMSSerializerBundle

假设你安装了它,你就可以做到

$serializedEntity = $this->container->get('serializer')->serialize($entity, 'json');

return new Response($serializedEntity);

您还应该检查StackOverflow上的类似问题:

  • 如何在Symfony 2.0 AJAX应用程序中将Doctrine实体编码为JSON?
  • Symfony 2 Doctrine导出到JSON
Vitalii Zurian answered 2019-09-10T10:25:01Z
56 votes

Symfony 2.1有一个JsonResponse类。

return new JsonResponse(array('name' => $name));

传入的数组将采用JSON编码,状态代码默认为200,内容类型将设置为application / json。

JSONP还有一个方便的setCallback功能。

jmaloney answered 2019-09-10T10:25:37Z
15 votes

从Symfony 3.1开始,您可以使用JSON Helper[http://symfony.com/doc/current/book/controller.html#json-helper]

public function indexAction()
{
// returns '{"username":"jane.doe"}' and sets the proper Content-Type header
return $this->json(array('username' => 'jane.doe'));

// the shortcut defines three optional arguments
// return $this->json($data, $status = 200, $headers = array(), $context = array());
}
Bettinz answered 2019-09-10T10:26:01Z
9 votes

要完成@thecatontheflat的答案,我建议你也将你的行动包裹在try ... catch区块内。 这将阻止您的JSON端点在异常时中断。 这是我使用的骨架:

public function someAction()
{
    try {

        // Your logic here...

        return new JsonResponse([
            'success' => true,
            'data'    => [] // Your data here
        ]);

    } catch (\Exception $exception) {

        return new JsonResponse([
            'success' => false,
            'code'    => $exception->getCode(),
            'message' => $exception->getMessage(),
        ]);

    }
}

这样,即使出现错误,您的端点也会始终如一地运行,并且您可以在客户端对其进行处理。

Slava Fomin II answered 2019-09-10T10:26:31Z
8 votes

如果您的数据已经序列化:

a)发送JSON响应

public function someAction()
{
    $response = new Response();
    $response->setContent(file_get_contents('path/to/file'));
    $response->headers->set('Content-Type', 'application/json');
    return $response;
}

b)发送JSONP响应(带回调)

public function someAction()
{
    $response = new Response();
    $response->setContent('/**/FUNCTION_CALLBACK_NAME(' . file_get_contents('path/to/file') . ');');
    $response->headers->set('Content-Type', 'text/javascript');
    return $response;
}

如果您的数据需要序列化:

c)发送JSON响应

public function someAction()
{
    $response = new JsonResponse();
    $response->setData([some array]);
    return $response;
}

d)发送JSONP响应(带回调)

public function someAction()
{
    $response = new JsonResponse();
    $response->setData([some array]);
    $response->setCallback('FUNCTION_CALLBACK_NAME');
    return $response;
}

e)在Symfony 3.x.x中使用组

在您的实体内创建组

<?php

namespace Mindlahus;

use Symfony\Component\Serializer\Annotation\Groups;

/**
 * Some Super Class Name
 *
 * @ORM    able("table_name")
 * @ORM\Entity(repositoryClass="SomeSuperClassNameRepository")
 * @UniqueEntity(
 *  fields={"foo", "boo"},
 *  ignoreNull=false
 * )
 */
class SomeSuperClassName
{
    /**
     * @Groups({"group1", "group2"})
     */
    public $foo;
    /**
     * @Groups({"group1"})
     */
    public $date;

    /**
     * @Groups({"group3"})
     */
    public function getBar() // is* methods are also supported
    {
        return $this->bar;
    }

    // ...
}

在应用程序的逻辑中规范化Doctrine对象

<?php

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
// For annotations
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;

...

$repository = $this->getDoctrine()->getRepository('Mindlahus:SomeSuperClassName');
$SomeSuperObject = $repository->findOneById($id);

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$encoder = new JsonEncoder();
$normalizer = new ObjectNormalizer($classMetadataFactory);
$callback = function ($dateTime) {
    return $dateTime instanceof \DateTime
        ? $dateTime->format('m-d-Y')
        : '';
};
$normalizer->setCallbacks(array('date' => $callback));
$serializer = new Serializer(array($normalizer), array($encoder));
$data = $serializer->normalize($SomeSuperObject, null, array('groups' => array('group1')));

$response = new Response();
$response->setContent($serializer->serialize($data, 'json'));
$response->headers->set('Content-Type', 'application/json');
return $response;
Avram Cosmin answered 2019-09-10T10:27:46Z
translate from https://stackoverflow.com:/questions/11714941/how-can-i-send-json-response-in-symfony2-controller