Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Symfony:通过从Ajax获取处理程序发送变量_Javascript_Php_Jquery_Ajax_Symfony - Fatal编程技术网

Javascript Symfony:通过从Ajax获取处理程序发送变量

Javascript Symfony:通过从Ajax获取处理程序发送变量,javascript,php,jquery,ajax,symfony,Javascript,Php,Jquery,Ajax,Symfony,我想在我的Symfony3.0.3项目中使用AJAX。 通信工作正常,但我无法从JS向处理程序获取变量。在JS处理程序的方向上,它工作得很好 我正试图通过“$request->query->get('id')”从请求中获取变量,但我只得到“null” 另一方面,我试图使用URL中的变量,但出现以下错误: 在CommonBundle:Default:index.html.twig第421行的模板呈现过程中引发异常(“缺少用于生成路由“admin_ajax.”的URL的一些必需参数(“id”)。 我

我想在我的Symfony3.0.3项目中使用AJAX。 通信工作正常,但我无法从JS向处理程序获取变量。在JS处理程序的方向上,它工作得很好

我正试图通过“$request->query->get('id')”从请求中获取变量,但我只得到“null”

另一方面,我试图使用URL中的变量,但出现以下错误:

在CommonBundle:Default:index.html.twig第421行的模板呈现过程中引发异常(“缺少用于生成路由“admin_ajax.”的URL的一些必需参数(“id”)。

我不介意使用一种或另一种解决方案(根据您的建议,我将使用最好的一种),但我仍然希望这两种错误的解决方案

JS

路由:

admin_ajax:
    defaults: { _controller: CommonBundle:Default:getClient }
    path:     /ajax/{id}
ajax_getclient:
    defaults: { _controller: CommonBundle:Default:getClient }
    path:     /ajax/{id}
    options:
        expose: true
处理程序:

public function getClientAction($id)
{
    $request = $this->container->get('request_stack')->getCurrentRequest();
    $isAjax = $request->isXMLHttpRequest();
    if ($isAjax)
    {
        $response = array("code" => 100, "success" => true, "id" => $request->query->get('id'));
        return new Response(json_encode($response));
    }
    $response = array("code" => 0, "success" => false);
    return new Response(json_encode($response));
}
编辑: 感谢Rim和Rvanlaak的回答,我使用了

JS

路由:

admin_ajax:
    defaults: { _controller: CommonBundle:Default:getClient }
    path:     /ajax/{id}
ajax_getclient:
    defaults: { _controller: CommonBundle:Default:getClient }
    path:     /ajax/{id}
    options:
        expose: true
请注意,“expose:true”选项是工作所必需的


这是因为细枝在javascript之前执行,所以他没有重新确认客户机id参数

我遇到了同样的问题,并使用FOSJSRoutingBundle解决了它。请参阅以下帖子: