Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

440
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda