Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

439
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda