【问题标题】:symfony 4 redirecttoroute() not working with fetchsymfony 4 redirecttoroute() 不能与 fetch 一起使用
【发布时间】:2019-11-18 07:10:21
【问题描述】:

如果我直接输入'/hellotest'来测试它是否有效,重定向! 但是当我使用 fetch 请求属性是正确的, 但不重定向。 我是新人,我错过了什么?:( 谢谢

/**
 * @Route("/hellotest",name="inde")
 */
public function index()
{
    return $this->redirectToRoute('node', ['id' => 1]);
}

/**
 * @Route("/crec", name="crec",methods={"POST"})
 */
public function crec(SessionInterface $session, Request $request)
{
    $data = $request->request->all();

    return $this->redirectToRoute('node', ['id' => $data['id']]);
}

/**
 * @Route("/node/{id}",name="node")
 */
public function nodeid(SessionInterface $session, $id)
{
    //I do something
}

在 Javascript 方面:

function send(id) {
    const data = new FormData();
    data.append('id', id);

    fetch('/crec', {
        method: 'POST',
        body: data
    })
}

【问题讨论】:

  • 您好,您在哪里使用send 方法? fetch 是什么?

标签: symfony fetch redirecttoroute


【解决方案1】:

在您的 Javascript 上使用 URLSearchParams,您正在尝试使用“multipart/form-data”,因此请改用“application/x-www-form-urlencoded”。 请注意,该代码尚未经过测试。

function send(id) {
    fetch("/crec", {
      method:"POST"
    , body:new URLSearchParams(`id=${id}`)
    })
}

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2018-07-05
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2020-07-03
    • 1970-01-01
    相关资源
    最近更新 更多