簡體   English   中英

jQuery ajax url參數drupal服務器

[英]jQuery ajax url parameters drupal server

我正在嘗試將內聯參數發送到其他服務器:

  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未經授權:缺少必需的參數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,
      ),
    ),
  );

嘗試這個:

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

感謝Madbreaks(為此付出了+1),將其移至RESTful URL(請參閱什么是最好的/常見的RESTful URL動詞和操作?以及如何創建不帶動詞的REST URL? ):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM