Javascript 在codeigniter中使用ajax加载php文件
我是网络开发新手。如何加载包含html的php文件以更改span内容。顺便说一下,我正在使用codeigniter 我总是出错 这是我的js文件Javascript 在codeigniter中使用ajax加载php文件,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,我是网络开发新手。如何加载包含html的php文件以更改span内容。顺便说一下,我正在使用codeigniter 我总是出错 这是我的js文件 $(document).ready(init); function init(){ $('#home').click(doAjax); } function doAjax(){ $.ajax({ data:{ 'n':1 },
$(document).ready(init);
function init(){
$('#home').click(doAjax);
}
function doAjax(){
$.ajax({
data:{
'n':1
},
type: 'get',
success: ajaxSuccess,
error: ajaxError,
url: 'http://localhost/index.php/main/ajax'
});
function ajaxSuccess(data, textStatus){
alert("sucess");
$('#main-span').load(data);
}
function ajaxError(data, textStatus){
alert("error");
$('#main-span').html('error');
}
}
控制器
public function ajax(){
$input = $this->input->get('n');
$input = '/application/views/student_main.php';
echo $input;
}
maincontent-这是我希望span更新的地方
<div class="container-fluid">
<div class="row">
<div class="col-sm-3 col-md-2 sidebar" id ="nav-sidebar">
<?php include('/application/views/navigation.php');?>
</div>
<span id="main-span">
<?php include('/application/views/evaluation.php');?>
</span>
</div>
</div>
提前感谢那些能够回答我问题的人。请尝试以下代码:
$.ajax({
url: "http://localhost/index.php/main/ajax",
type: "get",
data: {'n':1},
success: function(data,txtstatus){
alert("success");
$('#main-span').load(data.next_url);
},
error:function(){
alert("failure");
$('#main-span').html('error');
}
});
在控制器中:
public function ajax(){
$input = $this->input->get('n');
$input = '/application/views/student_main.php'; // you can't access view directly. call this view from a controller.
echo json_encode(array("next_url"=>$input));
}
不过,我还是犯了一个错误。我不知道它是什么,但当我检查错误时,它会说undefinedwhat is undefined?数据?抱歉,我不知道,但我只是在函数ajaxError中得到了responsetext,而我得到的所有信息都是未定义的