1

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 :)

Tony
  • 351
  • 3
  • 19
soul
  • 61
  • 8
  • Where do you see your unexpected message? Your browser console or so? (if yes can you paste us the result output?) Did you look at http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain ? – DFayet Mar 29 '17 at 22:05
  • Have you tried a more generic format like `application/xml`? – dbrumann Mar 30 '17 at 06:00
  • @dbrumann : tried but still NOK – soul Mar 30 '17 at 10:08
  • @DFayet : I see this in my browser console, the result output is just `SyntaxError: Unexpected token <`. I'm pretty sure it's a cross domain issue as my request works from within backend project – soul Mar 30 '17 at 10:29
  • If you look in the network tab you can have the full response. Also look http://stackoverflow.com/questions/37512870/ajax-cross-domain-on-symfony2 – DFayet Mar 30 '17 at 10:32

1 Answers1

1

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.. :/

soul
  • 61
  • 8