Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

441
Views
Symfony 3 Ajax call routing issue

I'm setting up a simple Ajax call in one of my forms. When a user enters characters in a field, the following Ajax call is activated:

self.modify = function (input_field) {
    if ($(input_field).val().length > 5) {
        $.post("{{path('get_bio_control_sample')}}", {sample_number: $(input_field).val()},
            function (response) {
                if (response.code == 100 && response.success) {
                    alert(response.sample_number);
                }
            }, "json");
    }
};

Which is meant to access the following controller action:

class BioControlController extends Controller {
    /**
     * @Route("/bio_control/sample", name="get_bio_control_sample")
     */
    public function getBioControlSampleAction(Request $request){

        $sample_number = $request->query->get('sample_number');

        $response = array("code" => 100, "success" => true, "sample_number" => $sample_number, "sample_data" => "test");

        return new JsonResponse($response);
    }
}

However, when the call is activated JS returns the error:

http://127.0.0.1:8000/omics_experiment/%7B%7Bpath('get_bio_control_sample')%7D%7D 404 (Not Found)

I'm accessing the Ajax call from omics_experiment/new (which is in the OmicsExperimentController) and using the route /bio_control/sample (as shown by the annotation), but it's not working. Can someone explain what I'm doing wrong?

I used this question as a template, the fact I'm using Symfony 3 might mean there are syntactic errors.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I just had to do this recently. I'm no expert on Symfony either, but since I just did this I may be able to help. Using Symfony is not really much different than doing it with a static URL. The main thing is to make sure that your controller and route are set up properly and working without AJAX, then you just need to use the path set in your route for the .post call.

And what makes it worse, is that it's really hard to test this type of interaction. Even your twig includes can cause it to fail if they are set up wrong.

Looking at your code again I think this may be the problem. Change this

    $.post("{{path('get_bio_control_sample')}}", {sample_number: 

to this

    $.post("/bio_control/sample", {sample_number: 

Because I think the way you have it is only good for twig templates, so if Symfony is not looking at your JQuery file like it does a twig template, then, it's not going to understand how to get the route.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!