我正在尝试在CodeIgniter 2.x中执行简单的JQuery AJAX页面加载
在不同的网络浏览器中,它似乎也有不同的反应。注意:这不是一个不正确的路径问题,页面工作和显示正常,我使用base_url,realurl,手动键入绝对和手动键入相对路径,没有一个100%成功。有时候我认为它是.htaccess文件,但是如果它在所有浏览器中都不起作用,而不仅仅是1。
现在可以在以下浏览器中使用:
我已经为希望提供帮助的人在线安排了一个干净的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(通过JQuery ajax在test.php视图中加载)
<?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的唯一方法是
和
在http://www.allforthecode.co.uk/dev/CodeigniterJQueryAJAXTest/试一试,然后尝试点1,2&amp; 3列在页面上并观看控制台输出)
ZIP:http://www.allforthecode.co.uk/dev/CodeigniterJQueryAJAXTest/CodeigniterJQueryAJAXTest.zip
答案 0 :(得分:0)
发现问题...浏览器adons / plugins ...
如果您在firefox&amp;上安装了adblock它会破坏许多AJAX查询。
就我而言,它是“AdBlock”。
关掉它们一切都很好......