1

I'm trying to send a value into a path to a Symfony Controller vía Ajax. I am sure that I'm wrong in sending, but I can't find the error

Let me share the code:

View

I've written

<script type="text/javascript">  
$("#my-select").change(function(event){
 var id=$("#my-select").val();  
 var path = "path('go_to_route/" + id+ "')";      
 var route="{{ "+path+" }}";
 alert(path); 
 $.ajax({
  url : route,
  data : {},
  type : 'POST',
  dataType : '',
  success : function(data) {
  alert(data);
  },                
  error : function(xhr, status) {
  alert('Error');
  },
  complete : function(xhr, status) {
  //alert('Finally');
  }
  });
}); 
</script>

Routing

In routing.yml

go_to_route:
path:     /my-route/{id}
defaults: { _controller: myBundle:MyController:myFunction }

This Action is in MyBundle/MyController

Controller

public function myFunctionAction($id){
return new Response($id);
}

Ajax always return "Error" message. I' think that the error happened while sending the value. I've tried, but it doesn't work. What am I doing wrong? This is my first week with Symfony. I'll be grateful for help.

7
  • You could try : var route= '/my-route/' + id on your javascript. Regards Commented May 30, 2017 at 18:58
  • @Albeis doesn't work Commented May 30, 2017 at 19:03
  • 1
    Have you debugged it? Is the request sent? By going to Network on development console... Commented May 30, 2017 at 19:05
  • what do you see in your alert(path);? Commented May 30, 2017 at 19:08
  • @simon.ro with my code: path('go_to_route/5') Commented May 30, 2017 at 19:20

2 Answers 2

1

Instead of

var path = "path('go_to_route/" + id+ "')";      
var route="{{ "+path+" }}";

Use:

var path = "{{ path('go_to_route', {'id': '__ID__'}) }}";       // Result: var path = "my_route/__ID__";
path = path.replace("__ID__", id);                              // Result: path equals "my_route/17" for example
Sign up to request clarification or add additional context in comments.

1 Comment

Works great! Thanks so much!
1
var path = "path('go_to_route/" + id+ "')";      

Is just a string. Twig doesnt parse that expression, because its not inside {{

You could do

var path ='{{ path("go_to_route", {'id': 'xxx'}) }}'; // /my-route/xxx
path = path.replace("xxx", id); // replace xxx with your id

You need that replace-value, because twig doesn't know our js-variable id.

And theres no need for that var route="{{ "+path+" }}";

All together:

<script type="text/javascript">  
$("#my-select").change(function(event){
 var id=$("#my-select").val();  
 var path ='{{ path("go_to_route", {'id': 'xxx'}) }}';
 path = path.replace("xxx", id);
 $.ajax({
  url : path,
  data : {},
  type : 'POST',
  dataType : '',
  success : function(data) {
  alert(data);
  },                
  error : function(xhr, status) {
  alert('Error');
  },
  complete : function(xhr, status) {
  //alert('Finally');
  }
  });
}); 
</script>

Alternative

A much cleaner approach would be to use https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

1 Comment

Works fine! Thanks for explication

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.