从Symfony2中的ajax获取数据

时间:2016-02-09 06:28:27

标签: ajax symfony

大家好我在我的javascript中有以下代码将POST请求发送到我的symfony应用程序控制器之一:

$( "#manage-products-modal" ).delegate("#manage-products-form","submit",function( event ) {
                  event.preventDefault();

                  var formdata = new FormData(this);
                  formdata.append("products", JSON.stringify(viewCtrl.dliProductsChanged));
                  var params = {
                        type: "POST",
                        data: formdata,
                        processData: false,
                        contentType: 'application/json',
                        url: app.base_uri + '/admin/discount-level/' + + viewCtrl.discountLevelItemId + '/manage-product/update',
                        success: function($data){

                        },
                  }

                  console.log(JSON.stringify(viewCtrl.dliProductsChanged));

                  $.ajax(params);
            });

这是我的控制器动作:

public function manageProductUpdateAction($discountLevelItemId, Request   $request)
{

    $em = $this->getDoctrine()->getEntityManager();
    $entity = $em->getRepository('CIInventoryBundle:DiscountLevelItem')->find($discountLevelItemId);
    $form = $this->createForm(new DiscountLevelItemCollectionType(), $entity);
    $form->bindRequest($request);

    $encoders = array(new XmlEncoder(), new JsonEncoder());
    $normalizers = array(new CustomNormalizer());
    $serializer = new Serializer($normalizers, $encoders);

    $data = $request->request->all();
    var_dump($request->request->all());die;
    var_dump($serializer->serialize($data['products'], 'json'));
    die;
}

我的问题是,当我转储并尝试获取数据时,它返回NULL或空数组。

那么似乎是什么问题?

感谢。

3 个答案:

答案 0 :(得分:0)

你在这里使用了响应。

use Symfony\Component\HttpFoundation\Response;

public function manageProductUpdateAction($discountLevelItemId, Request   $request) 
{

    $em = $this->getDoctrine()->getEntityManager();
    $entity = $em->getRepository('CIInventoryBundle:DiscountLevelItem')->find($discountLevelItemId);
    $form = $this->createForm(new DiscountLevelItemCollectionType(), $entity);
    $form->bindRequest($request);

    $encoders = array(new XmlEncoder(), new JsonEncoder());
    $normalizers = array(new CustomNormalizer());
    $serializer = new Serializer($normalizers, $encoders);

    $data = $request->request->all();
    return new Response($data);
}

答案 1 :(得分:0)

public function manageProductUpdateAction($discountLevelItemId, Request   $request)
{

    $em = $this->getDoctrine()->getEntityManager();
    $entity = $em->getRepository('CIInventoryBundle:DiscountLevelItem')->find($discountLevelItemId);
    $form = $this->createForm(new DiscountLevelItemCollectionType(), $entity);
    $form->bindRequest($request);

    $encoders = array(new XmlEncoder(), new JsonEncoder());
    $normalizers = array(new CustomNormalizer());
    $serializer = new Serializer($normalizers, $encoders);

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

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

答案 2 :(得分:0)

如果您通过请求传递Json,则需要将其作为请求中的内容并对其进行解码。

我并不完全知道你在做什么,但这里有一个var_dump,可以证明我的观点。

    public function manageProductUpdateAction($discountLevelItemId, Request   $request)
    {

        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository('CIInventoryBundle:DiscountLevelItem')->find($discountLevelItemId);
        $form = $this->createForm(new DiscountLevelItemCollectionType(), $entity);
        $form->bindRequest($request);

        $encoders = array(new XmlEncoder(), new JsonEncoder());
        $normalizers = array(new CustomNormalizer());
        $serializer = new Serializer($normalizers, $encoders);

//        $data = $request->request->all();
//        var_dump($request->request->all());die;
//        var_dump($serializer->serialize($data['products'], 'json'));
//        die;

        $content = $request->getContent(); // get raw json
        $jsonDecoded = json_decode($content);
        var_dump($json); exit;

    }