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 infront 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.

Can anyone please help?

share|improve this question

57% accept rate
feedback

2 Answers

up vote 0 down vote accepted

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().

share|improve this answer
Do not include / before your file name. – Vikas Naranje Nov 11 '11 at 10:55
Thanks, that worked – sisko Nov 11 '11 at 11:20
@sisko checkout my edited answer. If you follow these step then you don't need to created .php file to be called by your .js file. – Vikas Naranje Nov 11 '11 at 11:33
feedback

the way it is now it looks like your problem is the slash before the file name.. that means "domain web root"

share|improve this answer
Without the slash I get the following : testsite.co.uk/node/testscript.core.php. There isn't even a 'node' directory in the structure of my module – sisko Nov 11 '11 at 10:27
sounds like an url rewriting issue.. did you try typing the url that you want the ajax call to use in your browser to see if you get anything (or redirected) ? – mishu Nov 11 '11 at 10:32
I tried the full path of the script but still got errors – sisko Nov 11 '11 at 10:37
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.