views:

275

answers:

2

Hi,

I am working on an appliication which is running in cakephp and i am using ajax query inbetween.

In all the cases for all the ajax post i have used the url as

       var ht = $.ajax({
                                  type: "GET",
   url: "http://172.20.52.99/FormBuilder/index.php/forms/viewChoices/"+attribute_id,
                                async: false
                        }).responseText;
                        var myObject = eval('(' + ht + ')');

 is there any way in cakephp where i can give my base url as http://172.20.52.99/FormBuilder/index.php/
  and to call the baseurl in all the places i want.

Please give some suggestions . Thank you..

A: 

I assume I've the same scenario: for development, the site is available at localhost/cake, whereby in production the site is deployed in the root dir of example.com. In the header I set:

<base href="<?php echo $base_url ?>" />,

in AppContoller::beforeRender() I set:

$this->set('base_url', 'http://'.$_SERVER['SERVER_NAME'].Router::url('/'));.

This works fine for everything, except JS (so thus AJAX), as it ignores base_url.

Therefore I have a workaround (uses jQuery, but easy to substitute without):

forUrl = function(url) {
    return $('base').attr('href')+url.substr(1);
}

login = function() {
    $.post(forUrl('/ajax/profileDiv'), $('#profile-login-form').serialize(),
        function(data) {
            (...)
        });
}
sibidiba
A: 

You could set default URL for all your ajax requests that way:

$.ajaxSetup({
  url: 'http://172.20.52.99/FormBuilder/index.php/'
});
Pavel