I am trying to perform a simple JQuery AJAX page load within a CodeIgniter 2.x
It also seems to respond differently in different web browsers. NOTE: This is not an incorrect path issue, the pages work and display fine, I have used base_url, realurl, manually typed absolute and manually type relative paths and none give 100% success. At times I think is it the .htaccess file but if it was that it wouldn’t work in all browsers not just all but 1.
This is now working in the following browsers:
- Chrome – Doesn’t work
- IE 9 – Works
- FireFox – Works
- Safari – Works
- Opera – Works
I have placed a cleaned CI test site / application online for anyone wishing to help:
Demo (can be tested here too, try points 1,2 & 3 listed on page and watch console outputs):
http://www.allforthecode.co.uk/dev/CodeigniterJQueryAJAXTest/
Files:
/application/controllers/test.php
/application/views/test.php (loaded in by test.php)
/application/controllers/advert/test2.php (loaded in by the test.php view via JQuery ajax)
/application/controllers/test_root.php (loaded in by the test.php view via JQuery ajax)
/application/views/advert/test2.php (loaded in by test2.php)
testapp/application/controllers/test.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class test extends CI_Controller {
public function index()
{
$this->load->view('test');
}
}
?>
/application/views/test.php (loaded in by test.php controller)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>JQUERY AJAX TEST</title>
<!-- CSS START -->
<style>
html, body {
font-family: arial;
font-size: 12px;
}
#container {
width: 100%;
height: 400px;
border: 2px solid #000000;
overflow: auto;
}
</style>
<!-- JS INCLUDES START -->
<script type="text/javascript" src="<? echo(base_url(" includes/js/jquery/jquery-1.7.2.min.js
")); ?>"></script>
<script type="text/javascript" src="<? echo(base_url(" includes/js/functions.js
")); ?>"></script>
<!-- JQuery AJAX START -->
<script type="text/javascript">
function loadPage() {
$url = $("#url").val();
var request = $.ajax({
type: "post",
url: $url,
data: "t=1234",
success: function (data) {
trace("#### SUCCESS ####");
$("#container").html(data);
},
error: function (data) {
trace("#### ERROR ####");
trace("data = " + data);
traceObject(data);
traceToDiv("#container", "<h2>#### ERROR ####</h2>");
traceObjectToDiv("#container", data);
},
statusCode: function (data) {
trace("#### StatusCode ####");
traceObject(data);
traceToDiv("#### StatusCode ####");
traceObjectToDiv(data);
},
isModified: function () {
trace("#### isModified ####");
}
});
}
<!-- JQuery AJAX END -->
</script>
</head>
<body>
<form id="frm">
<input type="button" value="LOAD PAGE IN BOX" onclick="loadPage();" />
<input type="reset" value="RESET" />
<div id="container"></div>
<b>JQuery AJAX page url target:</b>
<input type="text" id="url" value="advert/test2"
/>
<ol>
<li>
<p>Set
<b>url target</b>to "test_root" to test load a root page. (WORKS IN CHROME!)</p>
</li>
<li>
<p>Set
<b>url target</b>to "advert/test2" to test load a 1 dir deep controller page.
(FAILS IN CHROME!)</p>
</li>
<li>
<p>Set
<b>url target</b>to "../ci.php" to test load a php page outside of the codeigniter
framework. (WORKS IN CHROME?!)</p>
</li>
</ol>
</form>
<br>
<h3>Test links</h3>
<ul>
<li>Browser test link via index.php/
<br/>
<a href="<? echo(base_url(" index.php/advert/test2 ")); ?>"><b><? echo(base_url("index.php/advert/test2")); ?></b></a>
</li>
<li>Browser test link direct (needs .htaccess to work)
<br/>
<a href="<? echo(base_url(" advert/test2 ")); ?>"><b><? echo(base_url("advert/test2")); ?> </b></a>
</li>
<li>Browser test link direct (needs .htaccess to work)
<br/>
<a href="<? echo(base_url(" test_root ")); ?>"><b><? echo(base_url("test_root")); ?></b></a>
</li>
<li>Browser test link direct
<br/>
<a href="../ci.php"><b>../ci.php</b></a>
</li>
</ul>
<br>
<p>Windows 7x64 - Browser results:</p>
<ul>
<li>Chrome - Fail</li>
<li>IE 9.0.8112.16421 64Bit - Success (Compatability view on & off)</li>
<li>IE 9.0.8112.16421 32Bit - Success (Compatability view on & off)</li>
<li>FireFox 14.0.1 - Success</li>
<li>Opera 12 build 1467 - Success</li>
<li>Safari 5.1.7 (7534.57.2) - Success</li>
</ul>
</body>
</html>
/application/controllers/advert/test2.php (loaded in by the test.php view via JQuery ajax)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class test2 extends CI_Controller {
public function index()
{
$this->load->view('advert/test2');
}
}
?>
/application/views/advert/test2.php (loaded in by test2.php)
Hello I am loaded by JQuery AJAX
../ci.php
<?
echo("I am a php file echo test from outside of the Codeigniter test site");
?>
Chromes network results are:
http://127.0.0.1/testapp/public/advert/test2 - Method:POST - Status: Failed
Additional
The only ways this code works for Chrome is
- If I use a html file outside the CodeIgniter framework for this project the JQuery AJAX load works. Any ideas why that would work in all other browsers bar Chrome?
and
- If I place the page which JQuery wants to load at the root of the CodeIgniter website. So “application/controllers/test.php” which loads “application/views/advert/test2.php” will work but “application/controllers/advert/test.php” will not? But works in all other browsers? Chrome status – 0
Anyone got any ideas?
Give it a try at http://www.allforthecode.co.uk/dev/CodeigniterJQueryAJAXTest/ and try points 1,2 & 3 listed on page and watch console outputs)
ZIP: http://www.allforthecode.co.uk/dev/CodeigniterJQueryAJAXTest/CodeigniterJQueryAJAXTest.zip
Found the problem… Browser adons/plugins…
If you have adblock installed on either firefox & chrome it will break many AJAX Queries.
In my case it was “AdBlock”.
Turn them off and all is fine…