我正在开发一个带有一些带有一些AJAX代码的jQuery脚本的模块。 ajax代码调用与jQuery脚本位于同一位置的php脚本。
我的问题是,AJAX在PHP脚本名称前面附加了域名,当然,我的脚本在该位置不存在,因此进程中断。
AJAX代码如下:
$(document).ready(
function(){
$.ajax({
url: "/testscript.core.php",
asych: false,
success: function($data){
$('textarea#edit-simplechat-messages').text( $data );
}
});
}
);
以下是firebug中显示的链接:
http://testsite.co.uk/testscript.core.php
再次,jQuery脚本和php脚本位于同一目录中。 我认为在我的php脚本名称之前的正斜杠会消除域名,但它不起作用。
答案 0 :(得分:0)
现在的方式看起来你的问题 是文件名前面的斜杠..这意味着“域名网络根目录”
答案 1 :(得分:0)
使用
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()中定义。