【问题标题】:Symfony + VueJs + Axios = No send form parametersSymfony + VueJs + Axios = 无发送表单参数
【发布时间】:2019-06-27 05:28:06
【问题描述】:

我正在使用 Symfony 4、VueJs 2 和 Axios 通过 Ajax 发送我的表单。表单发送没有明显问题,但我的表单的输入数据似乎没有随它一起发送。

好奇心:我做到了,但使用 jQuery Ajax 并且效果很好。

我已经尝试过这个 2017 年的解决方案,但对我不起作用:POST Requests with axios not sending parameters

index.html

<form id="form" @submit.prevent="sendForm">
    <input type="text" v-model="name">
    <input type="submit" value="Send">
</form>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    let form = new Vue({
        el: "#form",
        data: {
            name: ""
        },
        methods: {
            sendForm: function () {
                axios
                    .get("/send", this.data)
                    .then(response => console.log(response.data))
            }
        }
    });
</script>

homeController.php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class HomeController extends AbstractController {
    /**
     * @Route("/", name="home")
     */
    public function index() {
        return $this->render('home/index.html.twig');
    }

    /**
     * @Route("/send", name="send")
     */
    public function sendForm(Request $request) {
        $name = $request->query->get("name");
        return new Response($name);
    }
}

【问题讨论】:

    标签: php symfony vue.js axios


    【解决方案1】:

    在 Symfony 中尝试:$request-&gt;getContent()

    示例:

    // ...
    class HomeController extends AbstractController {
        //...
        /**
         * @Route("/send", name="send")
         */
        public function sendForm(Request $request) {
            // Decode data with json_decode()
            $data = json_decode($request->getContent(), true);
            $name = $data['name'];
    
            return $this->json($name);
        }
        //...
    }
    

    享受吧!

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 2018-11-06
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 2018-12-24
      • 2020-05-12
      • 2019-06-21
      • 1970-01-01
      相关资源
      最近更新 更多