jQuery ajax url parameters drupal server

13,908

Thanks to Madbreaks (+1 for it), moved to RESTful URLs (see What are the best/common RESTful url verbs and actions? and How to create REST URLs without verbs?):

  $services_resources['user']['relationships']['disps'] = array(
    'help'                    => t('Retrieves the disps for a given user'),
    'file'                    => array('type' => 'inc', 'module' => 'disps', 'name' => 'resources/disps.resource', ),
    'callback'                => '_disps_user_dispositivos',
    'access callback'         => '_disps_user_dispositivos_access',
    'access callback file'    => array('type' => 'inc', 'module' => 'disps', 'name' => 'resources/disps.resource', ),
    'access arguments'        => array('view'),
    'access arguments append' => TRUE,
    'args'                    => array(
      array(
        'name'          => 'account',
        'type'          => 'string',
        'description'   => 'The account to retrieve the cue from',
        'source'        => array('path' => 0, ),
        'optional'      => FALSE,
      ),
      array(
        'name'          => 'disp_id',
        'type'          => 'string',
        'description'   => 'The disp_id to retrieve the cue from',
        'source'        => array('param' => 'disp_id', ),
        'optional'      => FALSE,
      ),
    ),
  );

so now in the js there is

userAPI.disps_cue= function (disp_id, user, callback) {
  jQuery.ajax({
    type: "GET",
    url: this.apiPath + 'user/' + user + '/disps',
    dataType: 'json',
    data: {disp_id: disp_id,},
    success: callback,
  });
}

jQuery(document).ready(function($) {
  jQuery("#getCue").submit(function() {
    jQuery("#msg").html("Enviando datos..."));
    userAPI.disps_cue(jQuery("input:[name=disp_id]").val(), function(data) {
      jQuery("#result").append(CreateTableView(data, "box-table-b"));
      jQuery("#msg").html("Ok!");
    });
  });
});
Share:
13,908
Miquel
Author by

Miquel

GitHub: AdMobAds: Cordova/Phonegap/Intel-XDK plugin to show AdmobAds in hybrid HTML5 apps

Updated on June 04, 2022

Comments

  • Miquel
    Miquel over 1 year

    I'm trying to send inline parameters to a rest server:

      jQuery.ajax({
        type: "POST",
        url: this.apiPath + '/disp',
        dataType: 'json',
        data: 'disp_id=' +  disp_id,
        success: callback
      });
    

    Is there a way to pass parameters to a jQuery ajax? I've tried in a lot of manners but no way...

    data: {disp_id: disp_id},
    data: "{disp_id:" + '"' + disp_id + '"}',
    data: JSON.stringify({disp_id: disp_id}),
    

    Always the same answer: "401 Unauthorized: Missing required argument disp_id".

    The only way I achieved this is with:

      jQuery.ajax({
        type: "POST",
        url: this.apiPath + '/disp?disp_id=' + disp_id,
        dataType: 'json',
        success: callback
      });
    

    Extra details:

    this.apiPath = http://localhost/public_html/svc/disps

    On the server side (drupal) I've defined the following hook_services_resources:

      $services_resources['disps']['actions']['disp'] = array(
        'help'                    => t('Retrieves the cue of objects for a given id'),
        'file'                    => array('type' => 'inc', 'module' => 'disps', 'name' => 'resources/disps.resource', ),
        'callback'                => '_disps_resource_dispositivos',
        'access callback'         => 'disps_can_view_disp',
        'access arguments'        =>  array(NULL),
        'access arguments append' => FALSE,
        'args'                    => array(
          array(
            'name'          => 'disp_id',
            'type'          => 'string',
            'description'   => '',
            'source'        => array('param' => 'disp_id', ),
            'optional'      => FALSE,
          ),
        ),
      );