James
James

Reputation: 2860

AJAX function call within Joomla Component

I have a good understanding of AJAX and normally would have no problems using it but I am relatively new to Joomla and have only recently started building components, etc.

I have created a component (named directory) which is using the 'default' view. Within here I have the following code which is an AJAX call:

<script type="text/javascript">                         
var url = "index.php?option=com_directory&view=directory&task=clubFilter&format=raw";
jQuery(document).ready(function() {
    jQuery('#city').change(function() {         

        jQuery.ajax({
            url: url,
            type: "POST",
            data: jQuery("#city").serialize(),
            dataType: 'json',
            success: function(data) {
                alert('data');
            }
        });
    });
});

And within my 'views/directory/views.html' file I have created the following function which currently contains a die so I can confirm when it is working:

public function clubFilter() {
        die(print_r('here_i_am'));
    }

When I run the following code I ge the following error within Firebugs console..

'Error: 500 View not found [name, type, prefix]: directory, raw, directoryView'

I think it is because of the AJAX url var being incorrect but I have tried many different solutions from here nad other sources and just can't get the AJAX function working. Is my URL wrong? Many Thanks

Upvotes: 3

Views: 8911

Answers (2)

Joshua Dwire
Joshua Dwire

Reputation: 5443

Normally, I make ajax calls to a task on the controller.

Here is the url format I am using in one of my extensions that uses ajax calls to a component:

index.php?format=raw&option=<component_name_goes_here>&task=<task_goes_here>

Then, in my component's default controller I put a function with the same name as the task:

function getSomeData()
{
   echo(json_encode($data));//I normally return json
}

Hope this helps.

Upvotes: 5

Techie
Techie

Reputation: 45124

try this url. it might help you out.

var url = "index.php?option=com_directory&view=directory&task=clubFilter&tmpl=component";

format=raw replace with &tmpl=component

Else you can try the below format too.

jQuery.post('index.php',{
                        'option':'component_name',
                        'controller':'controller_name',
                        'task':'task_name',
                        'format':'raw',                   
                        'data': jQuery("#city").serialize(),
                        'dataType': 'json',
                },function(result){                     
                      //edit the result here

                    return;
       }); 

If you have any problem regarding this don't hesitate to ask.

Upvotes: 3

Related Questions