0

file mymodule.inc :

        $items['ajax/channel/%/get-program'] = array(
        'title' => t('get channel program by ID'),
        'type' => MENU_CALLBACK,
        'page callback' => 'get_channel_program',
        'page arguments' => array(2),
        'access arguments' => array('access content'),
      );

      return $items;
//channel program
function get_channel_program($channel_id){
  $channel_id = (isset($_POST['channel_id']) ? $_POST['channel_id'] : '');
  if (!is_numeric($channel_id)) :
    drupal_not_found();
  else :
    drupal_json_output($channel_id);
  endif;
}

file mudule.js :

Drupal.behaviors.ajaxProgram = {
        attach: function (context, settings) {

            // CSS Selector for the button which will trigger the AJAX call

            $('.channel-tv-tabs .tab .tabset .single-prog', context).click(function () {

                var channel_id = $(this).data('channel-id');

                $.ajax({
                    url: '/'+Drupal.settings.pathPrefix + 'ajax/channel/'+channel_id+'/get-program', // This is the AjAX URL set by the custom Module
                    type: "POST",
                    data: { channel_id : channel_id }, // Set the number of Li items requested
                    dataType: "json",          // Type of the content we're expecting in the response
                    success: function(data) {
                        var parsed_data = $.parseJSON(data);
                        console.log(parsed_data);  // Place AJAX content inside the ajax wrapper div
                    },
                    error: function(jqXHR, textStatus, errorThrown){
                        console.log(errorThrown);
                    }

                });

            });

        }
    };

Pls help me and tell what i do wrong? When I try go to link http://hostname/ru/ajax/channel/25/get-program I recive page not found

7
  • Not the issue, but why when $channel_id is automatically passed in do you grab it from POST again? Makes the data param in the ajax call redundant. Mar 16, 2016 at 12:27
  • 1
    @J.Reynolds I grab it from POST for example
    – user58175
    Mar 16, 2016 at 12:31
  • Yes but it is redundant as it is in the url as well. Either lose it in the url or in POST. Plus you are passing it over in post as an object. Mar 16, 2016 at 12:32
  • 1
    @J.Reynolds, You say about $channel_id = arg(2) ?
    – user58175
    Mar 16, 2016 at 12:36
  • 1
    @J.Reynolds, but i still recived 500 (Internal Server Error) from ajax
    – user58175
    Mar 16, 2016 at 12:41

1 Answer 1

1

Change the callback into this:

function get_channel_program($channel_id){
  if (!is_numeric($channel_id)) :
    drupal_not_found();
  else :
    drupal_json_output($channel_id);
  endif;
}

Change the jQuery into this:

$('.channel-tv-tabs .tab .tabset .single-prog', context).click(function () {
     ...
     $.get('/ajax/channel/'+channel_id+'/get-program', function(data) {
     alert("got "+ data);
   });
});

This uses HTTP GET verb.

According to the design of the HTTP specification, GET (along with HEAD) requests are used only to read data and not change it

instead of POST

The POST verb is most-often utilized to create new resources.

0

Your Answer

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