最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Symfony2: How to get the current route in javascript? - Stack Overflow

programmeradmin2浏览0评论

I'm using Ajax to change data in a page. So, I want to know that what is the current route in order to call to different functions. I have read many solutions used to retrieve the current url and also to get the current route in Controller and Twig. However, is there any possible way to achieve this in javascript or jQuery?

$(document).ready(function(){
    $('#form_patient').change(function(){
        var id = $(this).val();
        // Get the current route
        var route = ??;  // <----------------Want to get the current route
        if(route === 'route1'){
            functionForRoute2(id,route)
        }
        else{
            functionForRoute2(id,route);
        }
    });
});

** Function for the Route1 **

function functionForRoute1(id,route){
    $.ajax({
        type: "POST",
        url: Routing.generate(route),
        data: JSON.stringify({id:id}),
        dataType: "json",
        success: function(data){
            // Execute some specific data for route1
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert('Error : ' + errorThrown);
        }
    });
}

** Function for the Route2 **

function functionForRoute2(id,route){
    $.ajax({
        type: "POST",
        url: Routing.generate(route),
        data: JSON.stringify({id:id}),
        dataType: "json",
        success: function(data){
            // Execute some specific data for route2
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert('Error : ' + errorThrown);
        }
    });
}

I'm using Ajax to change data in a page. So, I want to know that what is the current route in order to call to different functions. I have read many solutions used to retrieve the current url and also to get the current route in Controller and Twig. However, is there any possible way to achieve this in javascript or jQuery?

$(document).ready(function(){
    $('#form_patient').change(function(){
        var id = $(this).val();
        // Get the current route
        var route = ??;  // <----------------Want to get the current route
        if(route === 'route1'){
            functionForRoute2(id,route)
        }
        else{
            functionForRoute2(id,route);
        }
    });
});

** Function for the Route1 **

function functionForRoute1(id,route){
    $.ajax({
        type: "POST",
        url: Routing.generate(route),
        data: JSON.stringify({id:id}),
        dataType: "json",
        success: function(data){
            // Execute some specific data for route1
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert('Error : ' + errorThrown);
        }
    });
}

** Function for the Route2 **

function functionForRoute2(id,route){
    $.ajax({
        type: "POST",
        url: Routing.generate(route),
        data: JSON.stringify({id:id}),
        dataType: "json",
        success: function(data){
            // Execute some specific data for route2
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert('Error : ' + errorThrown);
        }
    });
}
Share Improve this question asked Feb 17, 2014 at 22:57 lvarayutlvarayut 15.4k17 gold badges65 silver badges89 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

What I would do is to output route (any route you want) in a html tag for example (twig version):

<div id="my-route" data-route"{{ path("my_route") }}"></div>

Then in your code I would retrive that route via jquery like this:

$(document).ready(function(){
    $('#form_patient').change(function(){
        var id = $(this).val();

        var route = $('my-route').data('route');       
    });
});

You can also change path("my_route") to a string with a name of the route and then you are doing your if/else statement. However I dont think its a good idea as if your route name changes then your code will be affected as well

You will not get current route using just Javascript or JQuery. You can, however, get current URL with Javascript or current route using Twig.

Another possible solution is to issue one more AJAX call to server passing current URL, then match it to the correct route and send it back. However, if I were you, I would just get the current route from Twig.

I believe "window.location.href" would be a reasonable solution.

发布评论

评论列表(0)

  1. 暂无评论