猿问

Symfony 3.4 如何将 Javascript 数据传递给控制器​​?

我想在数据库中存储一个对象,其中包含动态内容。我有一个输入字段,人们可以在其中写文本。我希望输入字段的内容是对象的标题。我可以通过 Javascript 获取内容并通过控制器保存对象。但我不知道如何将数据从 Javascript 传递到控制器。


这是我已经尝试过的:


Javascript:



//Content I need to store

var outputOfInputField = document.getElementById("inputCategory").value;


//Ajax call with JSON that I think I need for pass data to controller.

$.ajax({

   url : '/createcategory',

   type: "POST",

   data: {

      'output': outputOfInputField

   },

   success : function (data) {

      console.log("SUCCES" + data);

   },

   error   : function () {

      console.log("ERROR");

   }

});

控制器:


<?php


namespace AppBundle\Controller;


use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\Routing\Annotation\Route;

use Symfony\Component\HttpFoundation\Response;

use AppBundle\Entity\User as User;

use Appbundle\Entity\Category as Category;



class HomePageController extends Controller

{

    /**

     * @Route("/", name="homepage")

     */


    public function getData() {

       ...//Not important

    }



    /**

     *  @Route("/create-category", name="newCat")

    */

    public function createObject() {


        //Tried to get the data from JSON file here, dont know what it does and also does not work, gives me an error... Saying i need a use on top of this controller. If I add it it still dont work.


        // $form = $this->get('form.factory')->createBuilder(FormType::class)

        //     ->add('ndf', CollectionType::class, array(

        //         'entry_type' => NoteDeFraisType::class,

        //         'label' => false,

        //         'allow_add' => true,

        //         'allow_delete' => true,

        //     ))

        //     ->getForm();


        //     if ($request->isXMLHttpRequest()) {

        //         $output = $request->request->get('output');

        //         echo $output;

        //         var_dump($output);

        //         print_r($output);

        //     }

    }

}


有只小跳蛙
浏览 110回答 1
1回答

慕姐4208626

我自己想出了解决方案:D。我不知道我的函数中需要 Request,现在也知道它的作用。javascript 保持不变,控制器现在看起来像这样:<?phpnamespace AppBundle\Controller;use Symfony\Bundle\FrameworkBundle\Controller\Controller;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\HttpFoundation\Response;use AppBundle\Entity\User as User;use Appbundle\Entity\Category as Category;class HomePageController extends Controller{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @Route("/", name="homepage")&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getData() {&nbsp; &nbsp; &nbsp; &nbsp;//...&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;*&nbsp; @Route("/create-category", name="newCat")&nbsp; &nbsp; */&nbsp; &nbsp; public function createCategory(Request $request) {&nbsp; &nbsp; &nbsp; &nbsp; // dump($request);&nbsp; &nbsp; &nbsp; &nbsp; // dump($request->request->get('output'));&nbsp; &nbsp; &nbsp; &nbsp; $output = $request->request->get('output');&nbsp; &nbsp; &nbsp; &nbsp; $category = $this->getDoctrine()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->getRepository('AppBundle:Category')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->findall();&nbsp; &nbsp; &nbsp; &nbsp; $category = new Category();&nbsp; &nbsp; &nbsp; &nbsp; $category->setTitle($output);&nbsp; &nbsp; &nbsp; &nbsp; $em = $this->getDoctrine()->getEntityManager();&nbsp; &nbsp; &nbsp; &nbsp; $em->persist($category);&nbsp; &nbsp; &nbsp; &nbsp; $em->flush();&nbsp; &nbsp; &nbsp; &nbsp; return new Response('Created product id '.$category->getId());&nbsp; &nbsp; }}我更改/添加的内容://Added Request $requestpublic function createCategory(Request $request) {//Added line for getting the output of ajax/json post.$output = $request->request->get('output');}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答