6

I'm a beginner in Symfony (version 2), I have a project achieved with plain basic PHP, and now I'm redoing my pages in dealing with Symfony framework, and arrived to my jquery ajax functions, surely, things gonna be different, I used to do like this:

$("#div").click(function(){
  $.post("targetFile.php",{/*parameters*/,function(data){ });
});

Q: How to make that works on Symfony? What to put instead of targetFile.php? a route most probably. and what to do on the controller and router sides? I looked out on Google and here, but didn't get any clear answers. Regards.

2
  • what are you having problems with? Commented Sep 26, 2011 at 12:23
  • How to make a Jquery Ajax function works on Symfony? Commented Sep 26, 2011 at 13:44

2 Answers 2

5

You realy just have to replace the targetFile.php by a custom route of yours.

So if you have this in your routing.yml:

# app/config/routing.yml
hello:
    pattern:      /ajax/target
    defaults:     { _controller: AcmeHelloBundle:Site:index }

You can use this javascript:

$("#div").click(function(){
  $.post("/ajax/target",{/*parameters*/,function(data){ });
});

On the Symfony2 side, the method indexAction of the SiteController of the AcmeHelloBundle will be called.

Sign up to request clarification or add additional context in comments.

3 Comments

look what Firebug tells me: Not Found The requested URL /target was not found on this server. while my router is: target: pattern: /target defaults: { _controller: WWBundle:Default:target} and my Javascript is $("#div").click(function(){ $.post("/target",{/*parameters*/,function(data){ }); }); and the controller is public function targetAction(){ return $this->render('WWBundle:Default:target.html.php'); } what's wrong Mr Damien?
Yes I did but still get this message: "NetworkError: 404 Not Found - localhost/target".
Adding that solved the problem: $isAjax = $this->get('Request')->isXMLHttpRequest(); really I don't understand how Symfony wants to work :), any way, thank you, I appreciate your support.
4

If you set inside routing.yml this:

_admin_ajax:
    resource: "@SomethingAdminBundle/Controller/AjaxController.php"
    type:     annotation
    prefix:   /admin/ajax  

... and inside controller, that will handle ajax call this:

/**
 * @Route("/ajaxhandler", name="_admin_ajax_handler")
 */
public function handlerAction() {
    
    $isAjax = $this->get('Request')->isXMLHttpRequest();
    if ($isAjax) {
        //...
        return new Response('This is ajax response');
    }
    return new Response('This is not ajax!', 400);
}

... then inside for example TWIG template you should call it like this:

$("#div").click(function(){
  $.post("{{ url('_admin_items_add') }}",{/*parameters*/,function(data){ });
});

... and the real route for your action will be generated with templating engine.

3 Comments

Well, I'll try,but what if the template is a php file? what to put as a url.
Adding that solved the problem: $isAjax = $this->get('Request')->isXMLHttpRequest(); really I don't understand Symfony logic :), any way, thank you, I appreciate your support bro.
And also clearing cache may affects, I can't say that it's the solution coz I was always clearing cache each time I change something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.