Drupal的模块jQuery的PHP脚本的位置问题脚本、模块、位置、问题

2023-09-10 21:22:29 作者:懒得讨好

我开发了与一些Ajax code一个jQuery脚本的模块。阿贾克斯code调用位于同一位置的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.

我的问题是,AJAX追加域名盈PHP脚本的名称,当然,我的脚本不能在该位置存在,所以整个过程中断。

My problem is, ajax appends the domain name infront of the php script name and of course, my script does not exist at that location and so the process breaks.

阿贾克斯code是如下:

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

再次,jQuery脚本和PHP脚本都在同一个目录。 我想正斜杠前,我的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.

任何人都可以请帮助?

推荐答案

使用

Drupal.settings.basePath

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

此链接可能是有用的。

http://www.akchauhan.com /多么神秘基路径的-Drupal的功能于使用Javascript /

编辑:

或者你也可以使用这种方法,如果您要创建自己的自定义模块,然后按照下列步骤

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

1]首先创建你的模块,在这里,我的模块名称为MyModule的,所以我创建了一个文件名的 mymodule.module

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]其次创建具有相同名称的js文件,因此将其命名为 mymodule.js 在同一个模块下的文件。

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){

            }
        });
    });  
}

请注意,在js文件我已经使用mypath中。您的js文件将调用),这条道路是在hook_menu(定义。

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