第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Symfony 3.4 如何將 Javascript 數(shù)據(jù)傳遞給控制器???

Symfony 3.4 如何將 Javascript 數(shù)據(jù)傳遞給控制器???

有只小跳蛙 2022-01-07 13:22:04
我想在數(shù)據(jù)庫中存儲一個對象,其中包含動態(tài)內(nèi)容。我有一個輸入字段,人們可以在其中寫文本。我希望輸入字段的內(nèi)容是對象的標(biāo)題。我可以通過 Javascript 獲取內(nèi)容并通過控制器保存對象。但我不知道如何將數(shù)據(jù)從 Javascript 傳遞到控制器。這是我已經(jīng)嘗試過的:Javascript://Content I need to storevar 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");   }});控制器:<?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{    /**     * @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);        //     }    }}
查看完整描述

1 回答

?
慕姐4208626

TA貢獻(xiàn)1852條經(jīng)驗 獲得超7個贊

我自己想出了解決方案:D。我不知道我的函數(shù)中需要 Request,現(xiàn)在也知道它的作用。


javascript 保持不變,控制器現(xiàn)在看起來像這樣:



<?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() {

       //...

    }


    /**

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

    */

    public function createCategory(Request $request) {

        // dump($request);

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


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


        $category = $this->getDoctrine()

            ->getRepository('AppBundle:Category')

            ->findall();


        $category = new Category();

        $category->setTitle($output);


        $em = $this->getDoctrine()->getEntityManager();

        $em->persist($category);

        $em->flush();


        return new Response('Created product id '.$category->getId());

    }

}

我更改/添加的內(nèi)容:


//Added Request $request

public function createCategory(Request $request) {

//Added line for getting the output of ajax/json post.

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

}


查看完整回答
反對 回復(fù) 2022-01-07
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號