Ajax网址不起作用+ Symfony2

nie*_*lsv 2 php ajax jquery symfony

我想用jquery autocomplete进行ajax调用,如下所示:

$("#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);
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

在我的routing.yml中,我定义了以下路由:

volley_scout_getteams_data:
pattern:  /team/getteams
defaults: { _controller: VolleyScoutBundle:Team:getteams }
Run Code Online (Sandbox Code Playgroud)

在我的TeamController中,我有一个名为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();
}
Run Code Online (Sandbox Code Playgroud)

(dump和die()仅用于测试,我想检查他是否可以找到链接).但是当我想进行ajax调用时,我总是会收到以下错误:

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

由于某种原因,他无法找到行动......有人知道我做错了什么吗?当我尝试这样的链接时:web/app_dev.php/team/getteams我得到了团队的转储..

更新: 我的javascript链接在基本视图(twig)中定义,如下所示:

{% block javascripts %}
    {% javascripts
        '@VolleyScoutBundle/Resources/public/js/jquery-1.10.2.min.js'
        '@VolleyScoutBundle/Resources/public/js/*'
    %}
        <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)

而ajax调用在我的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);
                }
            });
        }
    });
})();
Run Code Online (Sandbox Code Playgroud)

更新2: 我做了以下事情:

  • 安装捆绑包
  • 将捆绑包添加到我的AppKernel
  • 在app/config/routing.yml中注册了路由定义
  • 发布的资产(php app/console assets:install --symlink web)

将2条javascript行添加到我的base.html.twig,如下所示:

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

但现在我得到了这个错误:

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. 
Run Code Online (Sandbox Code Playgroud)

这很奇怪.当我清除缓存时,它第一次完美运行.当我刷新它显示错误...

Mic*_*ick 21

您的javascript代码中的以下行

url: "{{path('volley_scout_getteams_data')}}",
Run Code Online (Sandbox Code Playgroud)

不会工作......

最好的方法是使用 FOSJsRoutingBundle

1安装FOSJsRoutingBundle以在JavaScript代码中公开您的路由.(非常直截了当)

2 启用您的路线

volley_scout_getteams_data:
    pattern:  /team/getteams
    defaults: { _controller: VolleyScoutBundle:Team:getteams }
    options:
        expose: true
Run Code Online (Sandbox Code Playgroud)

3 适应你的js

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

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

  • 请安装一个`stable`版本的软件包,请参阅:https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/releases. (2认同)