无法使用AJAX获取POST数据

时间:2017-05-29 14:13:14

标签: ajax symfony doctrine symfony-3.1

我想在页面中显示4个对象,其中“加载更多”按钮每次单击时会显示4个对象。

我尝试将适用于(测试过)的PHP脚本改编为Symfony。 问题是我无法在Symfony函数中获取POST数据(页码),即使我可以在chrome开发人员工具栏中看到它...

POST variable with key 'page' and value '1' 我的控制器:

<?php
/**
 * Content controller.
 *
 * @Route("content")
 */
class ContentController extends Controller
{

/**
 * Lists all software.
 *
 * @Route("/software", name="content_software")
 * @Method({"POST", "GET"})
 */
public function softwareAction(Request $request)
{
    if ($request->request->get('page')){
        $page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT,
                FILTER_FLAG_STRIP_HIGH);
        $item_per_page = 4;
        $position = (($page_number-1) * $item_per_page);
        $contents = $this->getRepo()->findBy(array(),null,$item_per_page,$position);
    }
    else
    {
        $contents = "didn't work";
    }
    return $this->render('content/index.html.twig', array(
            'contents' => $contents
    ));
}
}

index.html.twig:

{%extends'loicCoreBundle :: Default / layout.html.twig'%}

{% block body %}
{{ dump(contents) }}
<script type="text/javascript">
    var track_page = 1; //track user click as page number, right now page number is 1
    load_contents(track_page); //load content

    $("#load_more_button").click(function (e) { //user clicks on button
        track_page++; //page number increment everytime user clicks load button
        load_contents(track_page); //load content
    });

        //Ajax load function
        function load_contents(track_page){

            $.post( "{{ path('content_software') }}", {'page': track_page}, function(data){

                if(data.trim().length == 0){
                    //display text and disable load button if nothing to load
                    $("#load_more_button").text("No more records!").prop("disabled", true);
                }
            });
        }           
</script>
{% endblock %}

3 个答案:

答案 0 :(得分:0)

试试这个:

$request->request->get('page')

而不是:

$request->request->has('page')

如果不起作用,请尝试:

var_dump($request->request->all());

检查变量

答案 1 :(得分:0)

我不确定你的错误来自哪里,我做了类似的测试,只是有效($request->request->get()没有问题)。

然而,您正在为每个子请求加载完整模板(index.html)。通常你想把像这样的调用分成api之类的方法,但是对于简单的事情来说它有点傻。

这是我测试的扩展/更新版本,我避免一起发布数据,只是使用该方法作为开关。这很好用,所以试着找出你错在哪里用这个作为反射(或者你喜欢用它做什么)。注意,这使用一个简单的PHP范围数组来测试数据,而不是实体,但它应该保持相同的原则。

控制器

    /**
     * @Route(
     *      "software/{page}/{limit}",
     *      name="content_software",
     *      requirements = {
     *          "page": "[1-9]\d*",
     *          "limit": "[1-9]\d*"
     *      }
     * )
     */
    public function softwareAction(Request $request, $page = 1, $limit = 4) {
        if ($request->isMethod('POST')) {
            // replace these two lines with database logic (SELECT & LIMIT)
            $start = ($page - 1) * $limit;
            $items = array_slice(range(1, 10), $start, $limit); // Just using a simple item array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as test data

            $content = '';
            foreach ($items as $item) {
                $content .= $this->get('twig')->render('someItemTemplate.html.twig', ['item' => $item]);
            }

            return new Response($content);
        } else {
            // alternatively you can send out the default items here for the first get request
            // and use the same item template above with {% embed %} to render them in this template
            return $this->render('someTemplate.html.twig');
        }
    }

someTemplate.html.twig

<html>
    <head>
        <title>Cake or death?</title>
    </head>
    <body>
        <ul id="put-the-things-here"></ul>
        <button id="next_please">Ehh cake please!</button>

        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script>
            var pageTracker = 1,
                $next = $("#next_please"),
                $target = $('#put-the-things-here');

            load_contents(pageTracker);

            $next.click(function (e) {
                load_contents(++pageTracker);
            });

            function load_contents(page) {
                // using post just for the request method (page is in the url)
                $.post("{{ path('content_software') }}/" + page,
                    function (data) {
                        if (data) {
                            $target.append(data);
                        } else {
                            $target.append('<li>We are out of cake.</li>');
                            $next.attr('disabled', true);
                        }
                    }
                );
            }
        </script>
    </body>
</html>

someItemTemplate.twig

<li>Cake: {{ item }}</li>

答案 2 :(得分:0)

我认为问题也来自路线。 我只使用&#34; POST&#34;时出现错误信息方法:

&#34;未找到&#34; GET / content / software&#34;的路由::方法不允许(允许:POST,DELETE)&#34;。 因为当我通过点击链接到达我的页面时,我会使用GET方法到达。

然而,当我点击我的&#34;加载更多&#34;按钮AJAX工作,我可以在我的symfony调试栏中看到我得到POST变量: Post variable

问题是当我第一次到达我的页面时,我想。

@Jenne van der Meer:谢谢,但我不想使用GET参数。

我添加完整的控制器代码,以防:

/**
 * Content controller.
 *
 * @Route("content")
 */
class ContentController extends Controller
{
    public function getRepo(){

        $em = $this->getDoctrine()->getManager();
        $repo = $em->getRepository('loicContentBundle:Content');

        return $repo;
    }

//  private $theRepo = getDoctrine()->getManager()->getRepository('loicContentBundle:Content');

    /**
     * Lists all content entities.
     *
     * @Route("/", name="content_index")
     * @Method("GET")    */
    public function indexAction(Request $request)
    {

        $contents = $this->getRepo()->findAll();
        $this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');

                $formFilter = $this->createFormBuilder()
                ->add('_',     EntityType::class,array(
                                'class' => 'loicFilterBundle:Filter',
                                'multiple' => true,
                                'expanded' => true,
                                'choice_label' => function($value) {
                                return ($value->getName());
                                },
                                ))
                        ->add('Appliquer filtres', SubmitType::class)

                                ->getForm();

                                $formFilter->handleRequest($request);
                                $data = '';
                                if ($formFilter->isSubmitted() && $formFilter->isValid()) {
                                    $data = $formFilter->getData();
                                    $data = $data['_']->toArray();

                                    $contents = $this->getRepo()->findAll();
                                }
                                else
                                    {$contents = $this->getRepo()->findAll();
                                    }

        return $this->render('content/index.html.twig', array(
                'contents' => $contents,'formFilter' => $formFilter->createView(),'request' => $request
        ));
    }

    public function contentAction($categoryId)
    {   
        $contents= $this->getRepo()->findBy(
                array('contentCategorycontentCategory' => $categoryId),
                null,
                4

                );  
        return $contents;
    }

    /**
     * Lists all software.
     *
     * @Route("/software", name="content_software")
     */
    public function softwareAction(Request $request)
    {
        var_dump($request->request->all());
        $bla =  $request->request->get('page');
        if ($request->request->get('page')){
            $page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT,
                    FILTER_FLAG_STRIP_HIGH);
            $item_per_page = 4;
            $position = (($page_number-1) * $item_per_page);
            $contents = $this->getRepo()->findBy(array(),null,$item_per_page,$position);
        }
        else
        {
            $contents = "didn't work";
        }
        return $this->render('content/index.html.twig', array(
                'contents' => $contents,'bla' => $bla
        ));

    }


    /**
     * Lists all videos.
     *
     * @Route("/videos", name="content_videos")
     * @Method("GET")
     */
    public function videoAction()
    {
        $contents = $this->contentAction(5);
        return $this->render('content/index.html.twig', array(
                'contents' => $contents,
        ));
    }

    /**
     * Lists all testimonies.
     *
     * @Route("/testimonies", name="content_testimonies")
     * @Method("GET")
     */
    public function testimoniesAction()
    {
        $contents = $this->contentAction(8);
        return $this->render('content/index.html.twig', array(
                'contents' => $contents,
        ));
    }

    /**
     * Lists all whiteBooks.
     *
     * @Route("/whiteBooks", name="content_whiteBooks")
     * @Method("GET")
     */
    public function whiteBooksAction()
    {
        $contents = $this->contentAction(3);
        return $this->render('content/index.html.twig', array(
                'contents' => $contents,
        ));
    }



    /**
     * Lists all actuality.
     *
     * @Route("/actuality", name="content_actuality")
     * @Method("GET")
     */
    public function actualityAction()
    {
        $contents = $this->contentAction(6);
        return $this->render('content/index.html.twig', array(
                'contents' => $contents,
        ));
    }

    /**
     * Lists all webinar.
     *
     * @Route("/webinar", name="content_webinar")
     * @Method("GET")
     */
    public function webinarAction()
    {
        $contents = $this->contentAction(4);
        return $this->render('content/index.html.twig', array(
                'contents' => $contents,
        ));
    }

    /**
     * Lists all blog posts.
     *
     * @Route("/blog", name="content_blog")
     * @Method("GET")
     */
    public function blogAction()
    {
        $contents = $this->contentAction(7);
        return $this->render('content/index.html.twig', array(
                'contents' => $contents,
        ));
    }


    /**
     * Creates a new content entity.
     *
     * @Route("/new", name="content_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {   
        $em = $this->getDoctrine()->getManager();
        $content = new Content();
        $form = $this->createForm('loic\ContentBundle\Form\ContentType', $content);        
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($content);
            $em->flush($content);

            return $this->redirectToRoute('content_show', array('id' => $content->getIdcontent()));
        }

        return $this->render('content/new.html.twig', array(
            'content' => $content,
            'form' => $form->createView(),
        ));
    }



    /**
     * Displays a form to edit an existing content entity.
     *
     * @Route("/{id}/edit", name="content_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Content $content)
    {
        $deleteForm = $this->createDeleteForm($content);
        $editForm = $this->createForm('loic\ContentBundle\Form\ContentType', $content);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('content_edit', array('id' => $content->getIdcontent()));
        }

        return $this->render('content/edit.html.twig', array(
            'content' => $content,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a content entity.
     *
     * @Route("/{id}", name="content_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, Content $content)
    {
        $form = $this->createDeleteForm($content);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($content);
            $em->flush();
        }

        return $this->redirectToRoute('content_index');
    }

    /**
     * Creates a form to delete a content entity.
     *
     * @param Content $content The content entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(Content $content)
    {
        return $this->createFormBuilder()
        ->setAction($this->generateUrl('content_delete', array('id' => $content->getIdcontent())))
            ->setMethod('DELETE')
            ->getForm()
        ;
    }
}

我的Bundle / Resources / config / routing.yml文件为空。