Welcome Guest, Not a member yet? Register   Sign In
Codeigniter, Base URL and Ajax
#1

[eluser]Unknown[/eluser]
Hello.

I've recently wrote my first CodeIgniter based project and i've encountered one issue i have no solution to.

The full content (except the header and footer) of my webpage is loaded via AJAX (my ajax calls a function in the default controller and outputs a view, simple enough).

The problem with base_url.

If i set the base url to (let's say): http://www.example.com , then the AJAX only works on http://www.example.com, and returns an "Access Denied" error on http://example.com.
If i change the base url to http://example.com, the ajax doesn't work on http://www.example.com.

The ajax call looks like this:
Code:
httpObject.open("GET", "<?php echo base_url()?>home/getnews/", true);

If the ajax call looks like this:
Code:
httpObject.open("GET", "home/getnews/", true);

the page loads fine unless the URI is different than http://www.example.com (or http://example.com)

Is there any tweak that i can use in order to avoid the "Access denied" error?

Thank you very much.
#2

[eluser]Bart v B[/eluser]
Where do you have your javascript defined?

Is it in the header of your view or an external .js?
In that way you have a problem, because you can not parse php in a javascript.

So, there are a few way's to solve this problem:

1. you put your script in your view.

Code:
<html>

<head>

<+script type="text/javascript">
httpObject.open("GET", "&lt;?php echo base_url();?&gt;home/getnews/", true);  <+/script>
&lt;/head


2. if it's an external .js then you must make an header to parse it to php..
something like this:

javascript.php

Code:
&lt;?php
header("Content-type: text/javascript");
?&gt;
httpObject.open("GET", "&lt;?php echo base_url()?&gt;home/getnews/", true);

and put offcourse this in your view page. Mind the extention. this is nou php Wink
#3

[eluser]Unknown[/eluser]
Nevermind, FIXED.

Thank you




Theme © iAndrew 2016 - Forum software by © MyBB