dongxing1853 2011-11-11 10:10
浏览 41
已采纳

Drupal模块jQuery PHP脚本位置问题

I am developing a module which has a jQuery script with some AJAX code. The ajax code calls a php script located in the same location as the jQuery script.

My problem is, AJAX appends the domain name in front of the PHP script name and of course, my script does not exist at that location and so the process breaks.

The AJAX code is as follows:

    $(document).ready(
    function(){

        $.ajax({
          url: "/testscript.core.php",
          asych: false,
          success: function($data){
            $('textarea#edit-simplechat-messages').text( $data );
          }
        });

    }
);

And the following is the link that shows up in firebug:

http://testsite.co.uk/testscript.core.php

Again, the jQuery script and the php script are in the same directory. I thought the forward slash before my php script name would eliminate the domain name but it did not work.

  • 写回答

2条回答 默认 最新

  • douqiang6448 2011-11-11 10:53
    关注

    Use

    Drupal.settings.basePath
    
    url: Drupal.settings.basePath+'your file path',
    

    This link might be useful

    http://www.akchauhan.com/how-know-base-path-of-drupal-in-javascript/

    EDIT :

    Or you can use this approach if you are creating your own custom module then follow these steps

    1] First create your module, Here my module name is "mymodule", So i created a file name 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] Secondly create js file with the same name so name it mymodule.js under the same module file.

    // $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){
    
                }
            });
        });  
    }
    

    Notice in js file i have used mypath. your js file will call this path which is defined in the hook_menu().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献