Search code examples
jqueryajaxsymfony1xmlhttprequestsymfony-1.4

Symfony and Ajax


This is my first time dabbling with Ajax in a Symfony context, and I am possibly a little confused by how the code must be organized.

Let's say my ajax call looks like this:

$.getJSON('module/getMeSomeJSONThroughAjax', function(data) {console.log(data); } )

and in my module/actions folder, the actions.class.php file has a method:

public function getMeSomeJSONThroughAjax()
{
   // do something
   return $jsonEncodedString;
}

Keep in mind, that I am not creating a template for the above method, hence I don't have the execute prefix as most other actions would. Simply coz I don't think creating a template is a case of overkill for fetching only JSON data for an AJAX HTTP request.

However, upon the event taking place in my browser that triggers the Ajax call, my console logs the following:

> Failed to load resource: the server responded with a status of 404
> (Not Found)
> http://localhost:8080/customerview_dev.php/flashcard/getMeSomeJSONThroughAjax

I am perceiving this to be a routing issue. A possible way to simply get rid of the problem would be to create a template for the above and rename the Action to executeGetMeSomeJsonThroughAjax(). But like I mentioned, this IMHO is overkill and there has to be a more aesthetically and proper way of getting this done.

What do you Symfonians do for making Ajax calls ?


Solution

  • You still need the execute prefix, even if you don't have a template. You can set the templates to None if you want, or:

    return $this->renderText($jsonEncodeString);
    

    should work too.