使用
Drupal.settings.basePath
url: Drupal.settings.basePath+'your file path',
这个链接可能有用
http://www.akchauhan.com/how-know-base-path-of-drupal-in-javascript/
编辑:
或者,如果您正在创建自己的自定义模块,则可以使用此方法,然后按照以下步骤操作
1] 首先创建你的模块,这里我的模块名是“mymodule”,所以我创建了一个文件名mymodule.module
<?php
function mymodule_init() {
drupal_add_js(drupal_get_path('module', 'mymodule') . '/mymodule.js');
// this call my js file when module is initialized.
}
function mymodule_menu(){
$items = array();
$items['mypath'] = array(
'title' => t('To get series of the selected brand'),
'page callback' => 'mymodule_page',
'page arguments' => array(1),
// get test_parameter from url, which is your first argument
//http://domain.com/mypath/test_parameter
// here mypath is arg(0), and test_parameter is arg(1)
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function mymodule_page($termID){
return drupal_json(array('message'=> $itemID));
}
2] 其次创建同名的js文件,将其命名为mymodule.js在同一个模块文件下。
// $Id$
Drupal.behaviors.mymodule = function (context) {
var $basepath = Drupal.settings.basePath;
$('selector').change(function(e){
$.ajax({
type: 'POST',
url: $basepath+'mypath/test_parameter',
// test_parameter :value you are sending to you module.
dataType:'json',
cache:false,
beforeSend:function(){
},
success:function(data){
alert(data.message);
},
complete:function(){
},
error:function(xhr, status, error){
}
});
});
}
在 js 文件中注意我使用了 mypath。你的 js 文件会调用这个在 hook_menu() 中定义的路径。