Drupal模块jQuery PHP脚本位置问题
我正在开发一个模块,它有一个带有一些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脚本名称之前的正斜杠将消除域名,但它不起作用。
使用
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
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()中定义的这个路径。
它现在的方式看起来你的问题是文件名之前的斜杠..这意味着“域web根”
- 何时在jquery中的ajax函数中使用async false和async true
- Chrome扩展程序内容脚本是否可以为html文件生成jQuery AJAX请求,该文件本身是Chrome扩展程序的一部分?
- Laravel Response :: json在浏览器中给出跨域错误,即使标题是Access-Control-Allow-Origin:*
- 使用JQuery的JQuery Cycle和JSON
- 在我的情况下,如何只发送一次AJAX调用
- 通过AJAX提交多个表单 – 同步或异步
- 在POST中使用base64编码的动画gif中的imagecreatefromgif()
- 使用jQuery访问多维JSON数组中的数据
- Jquery ajax调用不会访问控制器:无法加载资源:服务器响应状态为500(内部服务器错误)