我正在尝试在 CodeIgniter 2.x 中执行简单的 JQuery AJAX 页面加载
它似乎在不同的网络浏览器中也有不同的响应。注意:这不是一个不正确的路径问题,页面工作和显示都很好,我使用了 base_url、realurl、手动输入绝对路径和手动输入相对路径,但没有一个能 100% 成功。有时我认为它是 .htaccess 文件,但如果是它,它就不能在所有浏览器中工作,而不仅仅是 1。
现在可以在以下浏览器中使用:
- 铬 - 不工作
- IE 9 - 作品
- 火狐 - 作品
- Safari - 作品
- 歌剧 - 作品
我已经为任何希望提供帮助的人在线放置了一个干净的 CI 测试站点/应用程序:演示(也可以在这里进行测试,尝试页面上列出的第 1,2 和 3 点并观察控制台输出): http ://www.allforthecode.co .uk/dev/CodeigniterJQueryAJAXTest/
文件:
/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/应用程序/控制器/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(由 test.php 控制器加载)
<!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(由 test.php 视图通过 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(由 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 网络结果为:
http://127.0.0.1/testapp/public/advert/test2 - Method:POST - Status: Failed
额外的
此代码适用于 Chrome 的唯一方法是
- 如果我在该项目的 CodeIgniter 框架之外使用 html 文件,则 JQuery AJAX 加载有效。有什么想法可以在所有其他浏览器(除 Chrome 之外)中使用吗?
和
- 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