【问题标题】:jQuery ajax url parameters drupal serverjQuery ajax url 参数 drupal 服务器
【发布时间】:2012-03-09 18:24:12
【问题描述】:

我正在尝试将内联参数发送到休息服务器:

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

有没有办法将参数传递给 jQuery ajax? 我已经尝试了很多方式,但没有办法......

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

总是相同的答案:“401 Unauthorized: Missing required argument disp_id”。

我实现这一目标的唯一方法是:

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

额外细节:

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

在服务器端(drupal)我定义了以下 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,
      ),
    ),
  );

【问题讨论】:

  • 如果是 REST 服务器,我建议使用 RESTful URLs
  • 太棒了!是的,这让我重新思考......我要看看 GET 是否可行

标签: jquery ajax web-services drupal


【解决方案1】:

试试这个:

jQuery.post(this.apiPath + '/disp', {'disp_id':  disp_id}, callback);

【讨论】:

  • 相同结果:错误 401 并且参数未设置为参数,就像 POST 数据一样
【解决方案2】:

感谢 Madbreaks(为此 +1),迁移到 RESTful URL(参见 What are the best/common RESTful url verbs and actions?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,
      ),
    ),
  );

所以现在在 js 中有

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!");
    });
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-06
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多