jquery - Symfony2: How to get the current route in javascript? -
I am using ** ** Ajax
to change the data in a page. So, I want to know what is the current route to call for different tasks. I get current route in the current URL and get the current route in controller
and twig
Have read many solutions used to do However, javascript
or jQuery
? Any possible way to achieve this in $ [document] .ready (function) is {$ ('# Form_patient') .changes (function () {var id = $ ( This) .val (); // Current route var path = ??; // get & quot; -------- get -------- if you want to get current root (path = == 'Way1') {functionForRoute2 (id, route)} and {functionForRoute2 (id, route);}})}};
root 1
**
for function functionForRoute1 (ID, route) {$ .ajax ({type) Function: "Post", URL: routing, generate (route), data: JSON.stringify ({id: id}), data type: "json", success: function (data) {// path 1} Execute specific data, error: work (XMLHttpRequest, textStatus, errorThrown) {warning ('error:' error error);}}); }
root 2
**
function functionForRoute2 (id, route) {$ .ajax ({type: " Post ", url: routing (path), data: JSON.stringify ({id: id}), data type:" json ", success: function (data) {// execute certain data for path2} Error: function (XMLHttpRequest, textStatus, errorThrown) {warning ('error:' error error);}}); }
In an output route (that you want) for an example html Tag (Toggle Version):
& lt; Div id = "my-route" data-route "{{path (" my_route ")}}" & gt; & Lt; / Div & gt;
Then in your code I will return that route via jquery:
$ (document) .ready (function () { $ ('# Form_patient'). Change (function () {var id = $ (this) .val (); var route = $ ('my-route'). Data ('route');})}} ;
You can change the path ("my_route") into a string with the name of the path and then you are following the statement of / if. Although I do not think that this is a good idea as if your route name changes, your code will also be affected
Comments
Post a Comment