Welcome Guest, Not a member yet? Register   Sign In
ajax request to another controller - url trouble
#1

[eluser]dan.syme[/eluser]
js library: mootools

my ajax call:
Code:
window.addEvent('domready', function() {
    var path = <?php echo base_url(); ?> + 'index.php/sim_groups';
    //var group1 = $('group1');
    
    var request = new Request.JSON({
            url: path + '/get/1',
            onComplete: function(jsonObj) {
                alert('completed');
            }
        }).send();

});

on page load of of http://www.domain.com/index.php/events an ajax call to http://www.domain.com/index.php/sim_groups/get/1 triggers.

in firebug however the url returned has the events controller called in.

example: http://www.domain.com/index.php/events/i...oups/get/1

my sim_groups controller get() function is:

Code:
function get()
{
        
//Grab the group ID and set the table name.
$group = $this->uri->segment(3);
$table = 'group' . $group;
        
switch($group)
{
  case 1:
  $query = $this->db->get($table);
            
  foreach ($query->result() as $row)
  {
   $data[] = array('order'          => $row->order,
           'id'         => $row->id,
           'name'         => $row->name);
  }
            
  $json = '{"'. $table .'":' . json_encode($data) . '}';
  $data['json'] = $json;
  break;            
  }
                    
$this->load->view('sim_groups_response', $data);

}


I suspect that the view being loaded is causing the issue but I do need to have the view file intact, so I guess maybe I need to check for an ajax call? ideas?

Thanks,

Dan
#2

[eluser]WebsiteDuck[/eluser]
Change:
Code:
var path = <?php echo base_url(); ?> + 'index.php/sim_groups';
to:
Code:
var path = '<?php echo site_url(); ?>sim_groups';

Also I wouldn't call a view if you're just returning a json object...just echo the json object. But thats up to you.
#3

[eluser]dan.syme[/eluser]
Hi WebsiteDuck and thanks for your help.

That produced a call to the following location:

http://www.domain.com/index.php/events/<?php echo site_url(); ?>sim_groups/get/1

my changes below
Code:
window.addEvent('domready', function() {
    var path = '<?php echo site_url(); ?>sim_groups';
    var group1 = $('group1');
    
    var request = new Request.JSON({
            url: path + '/get/1',
            onComplete: function(jsonObj) {
                //addGroup1(jsonObj.group1);
                alert('completed');
            }
        }).send();

});

Also I need the view for other reasons.
#4

[eluser]WebsiteDuck[/eluser]
Is the javascript code in a .js file?
#5

[eluser]dan.syme[/eluser]
yep loaded into the events view via

standard script include
#6

[eluser]WebsiteDuck[/eluser]
Well there are a few things you can do...

You can set a global javascript variable path in your events view before you load your .js file.

or...

You can make the javascript load through a controller function, and then load it through the events view through that function (instead of "whatever.js" you'd include "/events/whatever") This will let you use php in your js file.

or...

You can hardcode the path into the js file instead
#7

[eluser]dan.syme[/eluser]
just fyi, i should have included that the call works fine when not using base_url or site_url. if I type the whole address out it works fine. if I use base or site_url it injects the events controller into the url
#8

[eluser]dan.syme[/eluser]
ya, just gonna hard code and move on and come back to it when i have more patience. thanks for your help mate.
#9

[eluser]WebsiteDuck[/eluser]
The reason it injects the events controller into the url is because the path doesn't start with a forward slash /

The browser thinks the path is supposed to be relative because it doesn't start with a forward slash, so then it tacks on http://www.domain.com/index.php/events/ before your path.

Note that in your first code
Code:
var path = <?php echo base_url(); ?> + 'index.php/sim_groups';
The php wasn't working, so your path was index.php/sim_groups

Which is why you ended up with http://www.domain.com/index.php/events/i...oups/get/1
#10

[eluser]tomcode[/eluser]
You can also use :

Code:
<base href="<?php echo base_url()" />
Edit : changed site_url() to base_url()

in the header of Your HTML file and use relative URL's for all assets. If really required, You still can grab that value in Your scripts.




Theme © iAndrew 2016 - Forum software by © MyBB