admin管理员组

文章数量:1388903

var prova= $("select#utente_regione_name").val();
$.ajax({
    type: "POST",
    url: "{{ path('province', { 'variabile': 'prova<------it's correct?' }) 
}}", 
var prova= $("select#utente_regione_name").val();
$.ajax({
    type: "POST",
    url: "{{ path('province', { 'variabile': 'prova<------it's correct?' }) 
}}", 
Share Improve this question edited Jul 15, 2012 at 10:41 hakre 198k55 gold badges449 silver badges856 bronze badges asked Jan 30, 2012 at 16:33 PopolitusPopolitus 4631 gold badge8 silver badges21 bronze badges 2
  • 3 Don't quote the variable name - that makes is a string "prova", not a variable. – Diodeus - James MacFarlane Commented Jan 30, 2012 at 16:38
  • wuaooo thx very muchhhhh!!!! -.- – Popolitus Commented Jan 30, 2012 at 16:44
Add a ment  | 

3 Answers 3

Reset to default 4

You can pass it as an attribute, like this:

var prova= $("select#utente_regione_name").val();
$.ajax({
    type: "POST",
    url: "{{ path('province') }}",
    data: { variable: prova },
}}", 

of course that means that in Controller you should read it from

$variable = $this->getRequest()->get('variable'); 

and remove that from your route. I don't think it's so crucial to have that parameter in route for Ajax routes.

PS. I would appreciate if somebody will post how to do this in a true Symfony way, because this still looks like workaround for me.

You can't access data which es in array $_POST in twig. You should change you script to pass variable from $_POST to you twig template.

Using FOSJsRoutingBundle will do the job fine. - that's precisely why it has been made for.

With it you can use router from JS:

Routing.generate('my_route_to_expose', { "id": 10, "foo": "bar" });

本文标签: phphow can pass javascript variable to twigStack Overflow