Welcome Guest, Not a member yet? Register   Sign In
ajax requests with jquery -- help
#1

[eluser]bryantAXS[/eluser]
So I'm having some trouble with getting some ajax calls to work using jQuery.

I had it working fine previously, but then I made some modifications to my .htacess file and a few more changes and now it seems to not be working. I think it has somthing to do with my .htaccess file - config.php and maybe routing.php?

It would be great if someone could explain how ajax calls work with codeigniter and how they are routed to the controller.

When my ajax call is fired in the page it looks to this url:

http://localhost:888/realm/index.php/sit.../twitterTL

It should be connecting to:

http://localhost:888/realm/index.php/ajax/twitterTL

actual ajax call

Code:
$.ajax({
  type: "GET",
  url: "ajax/twitterTL",
  data: "",
  dataType: 'json',
  success: function(data){
  self.addNewTweets(data);
  }
});

Any help and explanation would be greatly appreciated!

.htaccess

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /shop/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

config.php
Code:
$config['base_url'] = "http://localhost:8888/realm/";
$config['index_page'] = "";
$config['uri_protocol']    = "AUTO";

routes.php
Code:
$route['default_controller'] = "site";
$route['scaffolding_trigger'] = "";
#2

[eluser]bryantAXS[/eluser]
Another thing I noticed, is that when I navigate to the index.php file and it auto loads my Site controller, when i do the ajax call it works fine.

The error occurs when i click on the pagination, or when the controllers and functions are actually in the url. Then when i do the ajax call, the existing controller/function parameters stay in the url.

I guess this could be a javascript problem, and just the way jQuery handles the url attribute of the ajax object....hmm

any help or incite would still be appreciated!
#3

[eluser]iFadey[/eluser]
Recently I faced the same problem. I solved it by replacing the relative path in the ajax call with the absolute path. I mean try this:
Code:
$.ajax({
  type: "GET",
  url: "http://localhost:888/realm/index.php/ajax/twitterTL",
  data: "",
  dataType: 'json',
  success: function(data){
  self.addNewTweets(data);
  }
});
I know its not a good solution because you need to modify your js file whenever your base url changes Sad
#4

[eluser]iFadey[/eluser]
Or try one more thing. Define a base url in the head of your html document like this:
Code:
&lt;head&gt;
   ...
   &lt;base href="&lt;?php echo base_url(); ?&gt;" /&gt;
   ...
&lt;/head&gt;
Now try to make an ajax call like this:
Code:
$.ajax({
  type: "GET",
  url: "index.php/ajax/twitterTL",
  data: "",
  dataType: 'json',
  success: function(data){
  self.addNewTweets(data);
  }
});
That way your js file will not depend on the base url of your website Smile




Theme © iAndrew 2016 - Forum software by © MyBB