【问题标题】:Symfony3 send AJAX POST requestSymfony3 发送 AJAX POST 请求
【发布时间】:2016-08-09 21:09:37
【问题描述】:

我想通过 AJAX POST 发送两个变量 id 和 commentary 请求。 问题是我没有得到 POST 变量,但到达了路由。

JS:

$.post(Routing.generate('ajax_savecommentary', { id:id, commentary:commentary }), 
function(response)
{
}, "json");

Symfony:

public function saveCommentaryAction()
{
    if (!$this->get('session')->get('compte'))
        return $this->redirect($this->generateUrl('accueil'));

    $request = $this->container->get('request_stack')->getCurrentRequest();
    $isAjax = $request->isXMLHttpRequest();

    if ($isAjax)
    {
        $information = $this->getDoctrine()->getManager()->getRepository('CommonBundle:Information')->find($_POST['id']);

        $information->setCommentaire(str_replace('\n', '\\n', $_POST['commentary']));
        $this->getDoctrine()->getManager()->flush();

        $response = array("code" => 100, "success" => true, 'commentary' => $_POST['commentary']);
        return new Response(json_encode($response));
    }
    $response = array("code" => 0, "success" => false);
    return new Response(json_encode($response));
}

错误:

http://localhost/MyProject/web/app_dev.php/ajax/save/commentary/?id=61&commentary=MyCommentary.

{"code":0,"success":false}

更多 Symfony 错误:

GET Parameters

Key/Value

commentary/MyCommentary

id/61

并且路由是需要的:

ajax_savecommentary:
    defaults: { _controller: CommonBundle:Default:saveCommentary }
    path:     /ajax/save/commentary/
    options:
        expose: true

【问题讨论】:

    标签: javascript php ajax symfony


    【解决方案1】:

    尝试使用传递给控制器​​操作的请求,而不是从容器中检索它。所以试试这个:

    use Symfony\Component\HttpFoundation\Request;
    
    ...
    
    public function saveCommentaryAction(Request $request)
    {
        if (!$this->get('session')->get('compte'))
            return $this->redirect($this->generateUrl('accueil'));
    
        $isAjax = $request->isXMLHttpRequest();
    

    而不是这个:

    public function saveCommentaryAction()
    {
        if (!$this->get('session')->get('compte'))
            return $this->redirect($this->generateUrl('accueil'));
    
        $request = $this->container->get('request_stack')->getCurrentRequest();
        $isAjax = $request->isXMLHttpRequest();
    

    更新:

    您可以使用Customized Route Matching with Conditions 限制您的路由,例如您的案例如下:

    ajax_savecommentary:
        defaults: { _controller: CommonBundle:Default:saveCommentary }
        path:     /ajax/save/commentary/
        options:
            expose: true
        condition: "request.isXmlHttpRequest()"
        methods:  [POST]
    

    更新:

    JS端路由生成有错别字:

    $.post(Routing.generate('ajax_savecommentary', { id:id, commentary:commentary }), 
    function(response)
    {
    }, "json");
    

    您将数据作为 routing.generate 函数的参数传递,因此它将参数连接为查询字符串。所以试试这个:

    $.post(Routing.generate('ajax_savecommentary'), { id:id, commentary:commentary }, 
    function(response)
    {
    }, "json");
    

    另一个建议是使用 $request 对象而不是 PHP 超全局属性来获取数据,所以使用:

    $request->request-get('commentary');
    

    代替:

     $_POST['commentary']
    

    更多信息here in the doc。

    希望有帮助

    【讨论】:

    • 它对 isAjax 条件有效。但我仍然无法访问 $_POST 变量。实际上我发现它们是作为 Get 变量发送而不是发布的,如果我查看 $_GET['id'] 它就可以了。对我来说它仍然没有修复。
    • 嗨@ValentinBEAULE 抱歉,我没有看到您如何访问请求数据。在 symfony 中首选使用this 方法,所以不要使用超全局变量。
    • @ValentinBEAULE 您生成了错误的路由并将数据放入路由中,而不是作为第三个参数传递给 jquery post 函数
    • 感谢您的方法,我暂时使用它。关于路线,怎么错了?我确实可以访问该功能。你有例子吗?不要犹豫发布答案,因为您几乎已经解决了问题。
    猜你喜欢
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2015-06-22
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多