阿贾克斯网址动作不灵+ Symfony2的 [英] Ajax url to action not working + Symfony2

查看:133
本文介绍了阿贾克斯网址动作不灵+ Symfony2的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用jQuery自动完成这样一个Ajax调用:

  $(#register_player_team)。自动完成({
    来源:功能(请求,响应){
        $阿贾克斯({
            网址:{{路径('volley_scout_getteams_data')}},
            数据类型:JSONP
            成功:功能(数据){
                的console.log(数据);
            },
            错误:函数(XHR,ajaxOptions,thrownError){
                执行console.log(xhr.status);
                执行console.log(thrownError);
            }
        });
    }
});
 

在我的routing.yml我有以下路径定义的:

  volley_scout_getteams_data:
模式:/团队/ getteams
默认值:{_controller:VolleyScoutBundle:团队:getteams}
 

在我TeamController我有一个名为getteamsAction一个动作():

 公共职能getteamsAction()
{
    $的EntityManager = $这个 - > getDoctrine() - > getManager();
    //从数据库队
    $团队= $ entityManager-> getRepository('VolleyScoutBundle:团队') - >的findAll();

    的foreach($团队为$队){
        后续代码var_dump($队);
    }

    死();
}
 

(dump和死亡(),只是为了测试,我要检查,如果他能找到的链接)。 但是,当我想使Ajax调用我总是得到以下错误:

<$p$p><$c$c>http://localhost:8080/volleyscout/web/app_dev.php/user/%7B%7Bpath('volley_s…)%7D%7D?callback=jQuery110207641139030456543_1389372448462&_=1389372448463 404未找​​到)

出于某种原因,他无法找到行动...是否有人知道我在做什么错了?当我试着像这样的链接:网​​络/ app_dev.php /团队/ getteams 我得到了球队的转储。

更新: 我的JavaScript链接在基本视图(树枝)这样定义的:

  {%块的JavaScript%}
    {%Java脚本
        @ VolleyScoutBundle /资源/公/ JS / jQuery的,1.10.2.min.js
        @ VolleyScoutBundle /资源/公/ JS / *
    %}
        &所述;脚本的src ={{asset_url}}&GT;&所述; /脚本&GT;
    {%endjavascripts%}
{%端嵌段%}
 

和Ajax调用是在我page.js:

 (函数(){
    $(#register_userType)。改变(函数(){
        VAR值= $(本).find(选项:选择)。VAL();
        如果(价值=='P'||价值=='T'){
            $('团队')显示();
        }
        其他{
            $('团队')隐藏()。
        }
    });
    $(#register_player_team)。自动完成({
        来源:功能(请求,响应){
            $阿贾克斯({
                网址:{{路径('volley_scout_getteams_data')}},
                数据类型:JSONP
                成功:功能(数据){
                    的console.log(数据);
                },
                错误:函数(XHR,ajaxOptions,thrownError){
                    执行console.log(xhr.status);
                    执行console.log(thrownError);
                }
            });
        }
    });
})();
 

更新2: 我做了以下几件事:

  • 安装的软件包
  • 添加捆绑到我的AppKernel
  • 注册于应用程序的路由定义/配置/ routing.yml文件
  • 在发布资产(PHP应用程序/控制台资产:安装--symlink网)

增加了2行的JavaScript我base.html.twig这样的:

  {%块的JavaScript%}
    {%Java脚本
        @ VolleyScoutBundle /资源/公/ JS / jQuery的,1.10.2.min.js
        @ FOSJsRoutingBundle /资源/公/ JS / router.js
        @ VolleyScoutBundle /资源/公/ JS / bootstrap.min.js
        @ VolleyScoutBundle /资源/公/ JS / jQuery的-UI-1.10.3.custom.min.js
    %}
        &所述;脚本的src ={{asset_url}}&GT;&所述; /脚本&GT;
    {%endjavascripts%}
    &LT;脚本SRC ={{路径('fos_js_routing_js',{回调:fos.Router.setData})}}&GT;&LT; / SCRIPT&GT;
    &LT;脚本SRC ={{资产(包/ volleyscout / JS / security.js')}}&GT;&LT; / SCRIPT&GT;
{%端嵌段%}
 

但现在我得到这个错误:

  GET的http://本地主机:8080 / volleyscout /网络/ app_dev.php / JS /路由回调= fos.Router.setData 500(内部服务器错误)注册:117
未捕获的错误:路径volley_scout_getteams_data不存在。
 

这是非常奇怪的。当我清楚我的缓存,它第一次完美的作品。当我刷新它显示的错误...

解决方案

在JavaScript code以下行

  URL:{{路径('volley_scout_getteams_data')}},
 

将无法正常工作......

最好的办法是使用<大骨节病> FOSJsRoutingBundle

<大骨节病> 1 安装<大骨节病> FOSJsRoutingBundle 揭露你的路由在你的JavaScript $ C $℃。 (很直接的)

<大骨节病> 2 启用路线

  volley_scout_getteams_data:
    模式:/团队/ getteams
    默认值:{_controller:VolleyScoutBundle:团队:getteams}
    选项​​:
        揭露:真
 

<大骨节病> 3 适应你的JS

  VAR getTeamsUrl = Routing.generate('volley_scout_getteams_data');

$(#register_player_team)。自动完成({
    来源:功能(请求,响应){
        $阿贾克斯({
            网址:getTeamsUrl,
            // ...
        });
    }
});
 

I want to make an ajax call with jquery autocomplete like this:

$("#register_player_team").autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "{{path('volley_scout_getteams_data')}}",
            dataType: "jsonp",
            success: function( data ) {
                console.log(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr.status);
                console.log(thrownError);
            }
        });
    }
});

In my routing.yml I have the following route defined:

volley_scout_getteams_data:
pattern:  /team/getteams
defaults: { _controller: VolleyScoutBundle:Team:getteams }

And in my TeamController I have an action called getteamsAction():

public function getteamsAction()
{
    $entityManager = $this->getDoctrine()->getManager();
    // Get teams from database
    $teams = $entityManager->getRepository('VolleyScoutBundle:Teams')->findAll();

    foreach($teams as $team){
        var_dump($team);
    }

    die();
}

(The dump and die() is just for testing, I want to check if he can find the link). But when I want to make the ajax call I always get the following error:

http://localhost:8080/volleyscout/web/app_dev.php/user/%7B%7Bpath('volley_s…)%7D%7D?callback=jQuery110207641139030456543_1389372448462&_=1389372448463 404 (Not Found) 

For some reason he can't find the action ... Does someone knows what I'm doing wrong? And when I try the link like this: web/app_dev.php/team/getteams I get a dump of the teams ..

UPDATE: My javascript links are defined in base view (twig) like this:

{% block javascripts %}
    {% javascripts
        '@VolleyScoutBundle/Resources/public/js/jquery-1.10.2.min.js'
        '@VolleyScoutBundle/Resources/public/js/*'
    %}
        <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}

And the ajax call is in my page.js:

(function () {
    $("#register_userType").change(function(){
        var value = $(this).find("option:selected").val();
        if(value == 'P' || value == 'T'){
            $('.teams').show();
        }
        else{
            $('.teams').hide();
        }
    });
    $("#register_player_team").autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "{{path('volley_scout_getteams_data')}}",
                dataType: "jsonp",
                success: function( data ) {
                    console.log(data);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    console.log(xhr.status);
                    console.log(thrownError);
                }
            });
        }
    });
})();

UPDATE 2: I did the following things:

  • Installed the bundle
  • Added the bundle to my AppKernel
  • Registered the routing definition in app/config/routing.yml
  • Published assets (php app/console assets:install --symlink web)

Added the 2 javascript lines to my base.html.twig like this:

{% block javascripts %}
    {% javascripts
        '@VolleyScoutBundle/Resources/public/js/jquery-1.10.2.min.js'
        '@FOSJsRoutingBundle/Resources/public/js/router.js'
        '@VolleyScoutBundle/Resources/public/js/bootstrap.min.js'
        '@VolleyScoutBundle/Resources/public/js/jquery-ui-1.10.3.custom.min.js'
    %}
        <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
    <script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
    <script src="{{ asset('bundles/volleyscout/js/security.js') }}"></script>
{% endblock %}

But now I get this errors:

GET http://localhost:8080/volleyscout/web/app_dev.php/js/routing?callback=fos.Router.setData 500 (Internal Server Error) register:117
Uncaught Error: The route "volley_scout_getteams_data" does not exist. 

It's very strange. When I clear my cache, the first time it works perfectly. And when I refresh it shows the errors ...

解决方案

The following line in your javascript code

url: "{{path('volley_scout_getteams_data')}}",

won't work...

The best way is to use the FOSJsRoutingBundle

1 Install FOSJsRoutingBundle to expose your routing in your JavaScript code. (very straight-forward)

2 Enable your route

volley_scout_getteams_data:
    pattern:  /team/getteams
    defaults: { _controller: VolleyScoutBundle:Team:getteams }
    options:
        expose: true

3 Adapt your js

var getTeamsUrl = Routing.generate('volley_scout_getteams_data');

$("#register_player_team").autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: getTeamsUrl,
            //...
        });
    }
});

这篇关于阿贾克斯网址动作不灵+ Symfony2的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆