error 403 forbidden when sending ajax request

Hi again.

I'm developing a locations plugin for my project

I have a select box for states that lets user select a state from available options. Whenever the user makes a selection an ajax request is send to server for fetching a list of cities for a specific state. I'm using firebug for debugging ajax calls. I'm receiving 403 error and see no results.

I have this snippet in my start.php file:

elgg_register_ajax_view('locations/cities');

and here is my snippet code for ajax request:

$("#states").change(function() {
        stateId = $("#states").val();
        $.ajax({
            type: 'GET',
            dataType: 'html',
            cache: false,
            url: "<?php echo elgg_get_site_url(); ?>"+'ajax/view/locations/cities?stateId='+stateId,
            success: function(data) {
                alert('Call Success');
            }
        });
    });

 

and finally here is the content of cities.php ajax view (mod/locations/views/locations/cities.php):

state_id = (int) get_input('state_id');
echo "This is a test";

 

I studied the galliComments plugin and much of these code is similar to it, But I wonder why i am receiving this error?

  • Did you forget to make default folder?

    mod/locations/views/default/locations/cities.php

  • The problem was that I placed the call to elgg_register_ajax_view in page_handler function. It should be placed in init function.

    But there is another problem. I don't receive output from this call. The output is empty.

    $("#states").change(function() {
            stateId = $("#states").val();
            $.ajax({
                type: 'GET',
                dataType: 'html',
                cache: false,
                url: "<?php echo elgg_get_site_url(); ?>"+'ajax/view/locations/cities?stateId='+stateId,
                success: function(data) {
                    if(data.length>0){

                           alert(data);

                    }
                }
            });
        });

  • Just open the URL in your browser http://example.com/ajax/view/locations/cities?stateId=10

    If you don't get an error, the view isn't registered. If you get nothing, maybe you're missing the echo before the elgg_view() in your view.

    If you get what you want, then I'm not sure what's going on. You can simplify the ajax call:

    elgg.get('ajax/view/locations/cities?stateId=' + stateId, function (data) {
        console.log(data); // watch your console
    });
Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking