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

224
Views
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 answers
Answer question

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 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!