Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.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
从PHP控制器中获取数据_Php_Jquery_Symfony_Get - Fatal编程技术网

从PHP控制器中获取数据

从PHP控制器中获取数据,php,jquery,symfony,get,Php,Jquery,Symfony,Get,我试图通过$.get在jQuery中获取一些数据。我有一个链接,点击后,我想在控制器中接收数据。我做了这个,但不起作用,$\u GET数组为空: html.twig: <a href="{{path('current')}}" id="get">Some name</a> <script> $(document).ready(function() { $(document).on('click','#ge

我试图通过$.get在jQuery中获取一些数据。我有一个链接,点击后,我想在控制器中接收数据。我做了这个,但不起作用,$\u GET数组为空:

html.twig:

<a href="{{path('current')}}" id="get">Some name</a>
        <script>
        $(document).ready(function() {
            $(document).on('click','#get', function(){
                $.get({{path('current', {'id' : 'a1'})}}, function( data ) {
                console.log(data);
            });
            });

        });

        </script>

路由名称没有问题。可能还有其他一些想法如何通过单击链接获取控制器中的数据?我不想使用$.ajax,如果您只想让服务器接收请求,就不需要任何ajax。您可以使用具有相同参数的链接:

<a href="{{path('current', {'id' : 'a1'})}}" id="get">Some name</a>

保持您的小树枝上没有javascript,并使用data属性检索路径:

<a href="#_" data-posturl="{{path('current', {'id' : 'a1'})}}" id="get">Some name</a>

在您的操作中,如果在路由中定义了id,您应该能够将id作为操作的参数。否则它将在请求中可用。

这是关于symphony的问题吗?(我从名称
html.twig
)如果是这样,请将其标记为这样。您是否使用firebug进行调试?请在firebug控制台中检查请求参数、响应结果和请求url。通过firebug找出发生了什么。如果由twig呈现,这部分:
$.get({{path('current',{id':'a1'}}}},
将生成无效的javascript(缺少引号)。不要在symfony控制器内使用
$\u get
,使用
xxxAction(Request$Request){$val=$Request->query->get('key'));…
该键取决于路由的配置方式。如果它不是路由定义的一部分,它将只是查询的一部分(
$request->query)
。例如
/some dir/{notId}
。但是如果它是路由的一部分(
/some dir/{id}
),则必须将其注入方法。例如
xxxAction($id)
public function currentAction(Request $request, $id) {
    // $id - is your parameter
}
<a href="#_" data-posturl="{{path('current', {'id' : 'a1'})}}" id="get">Some name</a>
$(document).on('click','#get', function(){
                $.post($(this).data('posturl')).done(function (data) {
        }).error(function (data) {
            $('body').prepend(data.responseText);
        });});