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

227
Visualizações
ajax request on project 1 call project 2 symfony return Unexpected token <

I have 2 projects, let's call them backend and frontend. Backend is a Symfony3 project and Frontend is a simple html page with javascript in it.

I would like to call a peculiar url of the backend from the frontend so that it would return a string (a url).

So what I tried to do is a simple ajax get request with the backend url I would like to request but this returns

SyntaxError: Unexpected token "<"

(for html dataType) and

SyntaxError: Unexpected token.

(for json and jsonp dataType).

I tried with dataType json, jsonp, html adapting each time the controller response to answer respectively json, json, and html.

When I try this request from postman everything works just fine and i get either my json or html.

There is obviously something I don't understand.

Is there anybody that could explain me what I do wrong and how I could do it.

Below is my frontend project ajax request (for html in that case but I've let the commented json part I've jused for test in case of...)

$.ajax({
                url: 'http://sevignemiroir.local/display',
                type: 'GET',
                dataType:  "html",
                //dataType: "jsonp"
                success: function(response) {
                    console.log('success');
                    console.log(response);
                },
                error: function (response) {
                    console.log('error');
                    console.log(response);
                }
            })

And this my response from my backend symfony controller

public function indexAction()
{
    //return $this->json('toto');
    return $this->render('display/test.html.twig', [
        'toto' => 'toto'
    ]);
}

the test.html.twig file :

<div>test</div>

I am a bit confused :

  • Do I have to apply a specific format to the response to make it work?
  • Is there something I should configure in my controller to allow request from outside the project?
  • Does the problem come from a Cross-Domain issue?
  • I've heard about FOSRestBundle, is it mandatory to use such bundle to enable data send from outside the project?
  • What are the best practices for calling data from a project to another project?

These are the questions I'm getting confused with and if anyone would be kind enough to explain it, that would be greatly appreciated!

I don't know if that's important but I'm using MAMP and both project have virtualhosts configured

Many thanks to those that will take time to read this :)

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It now works but... I don't know why. I was installing various bundle on symfony (FOSRest, NelmioCors, JMS) and tried a jquery cross-plugin plugins, nohing was working but when I removed al of them, it now worked.. :/

about 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