ci里ajax的url直接访问php页面有返回值,url含控制器后没法返
本帖最后由 sevenk 于 2012-5-4 16:54 编辑页面代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ci ajax demo</title>
<script type="text/javascript" src="<?php echo base_url().'admincp/'.setting('backend_theme').'/'; ?>js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
$("#login_submit").click(function(){
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
type: 'post',
//dataType: 'json',
//cache: false,
//url: " http://127.0.0.1:81/admincp/default/test_types_ajax.php",//这个可以得到返回值
url: "<?php echo site_url('admin/test/post_action'); ?>",//这个没法得到返回值
data: 'username=' + username + '&password=' + password,
success:
function(msg){
alert("ok");
$("#form_message").html(msg.message).css({"background-color": msg.bg_color}).fadeIn('slow');
},
error: function() {alert("ajax error");}
});
return false;
});
});
</script>
<style type="text/css">
body{
background-color: #fff;
margin: 40px;
font-family:Arial, Helvetica, sans-serif;
font-size: 14px;
color: #4f5155;
}
a{
color: #003399;
background-color:transparent;
font-weight:normal;
}
h1{
color: #444;
background-color:transparent;
border-bottom:1px solid #d0d0d0;
font-size:16px;
font-weight:bold;
margin: 24px 0 2px 0;
padding: 5px 0 6px 0;
}
code{
font-family:Monaco, Verdana, Arial;
font-size:12px;
background-color:#f9f9f9;
border:1px solid #d0d0d0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#form_message{
display:none;
text-align:center;
margin-bottom:5px;
padding:10px;
}
</style>
</head>
<body>
<h1>This is the demo page ofr the simple AJAX post in CodeIgniter Using jQuery</h1>
<p>Username: stamhe | Password: 123456</p>
<p>
Try to enter nothing in the fields and click the submit button.<br/>
Try to mismatch the username and/or password and click the submit button.
</p>
<div style="text-align:right; width:500px;">
<div id="form_message"></div>
<form name="ajax_form" id="ajax_form" method="post">
Username/Email:*<input type="text" name="username" id="username" size="30" /><br/><br/>
Password:*<input type="password" name="password" id="password" size="30" /><br/><br/>
<input type="submit" value="Submit" name="login_submit" id="login_submit" />
</form>
</div>
</body>
</html>
控制器代码
<?php
class Test extends Admin_Controller {
public function __construct(){
parent::__construct();
}
function index()
{
$data['baseurl'] = $this->config->item('base_url');
$this->load->view('ajax_post', $data);
}
function post_action()
{
$message = "11";
$bg_color = "22";
$output = array(
'message' => $message,
'bg_color' => $bg_color
);
echo json_encode($output);
}
}
?>
admincp/default/目录下的test_types_ajax.php文件代码
<!-- start contact_form div-->
safwasdfs
<!-- end contact_form-->
firebug返回 404 not found ,在浏览器直接用控制器的方法,是可以得到返回值 {"message":"11","bg_color":"22"}的。
border=0
admin/test/post_action,你这个写的显然不对。 本帖最后由 sevenk 于 2012-5-4 17:01 编辑
yuzhigang5460 发表于 2012-5-4 16:55 static/image/common/back.gif
admin/test/post_action,你这个写的显然不对。
是dilicms的,而且我取消了index.php,可以不用加index.php访问的。在firebug得到的地址和错误是
POST http://127.0.0.1:81/admin/test/post_action
404 Not Found
chrome://firebug/content/blank.gif 154ms
这个地址在浏览器可以访问,返回值是{"message":"11","bg_color":"22"} sevenk 发表于 2012-5-4 17:00 static/image/common/back.gif
是dilicms的,而且我取消了index.php,可以不用加index.php访问的。在firebug得到的地址和错误是这个地址 ...
你自己看下html里site_url('admin/test/post_action')输出的url地址是什么。
这个用firebug很容易就看出来为什么了吧 yuzhigang5460 发表于 2012-5-4 17:03 static/image/common/back.gif
你自己看下html里site_url('admin/test/post_action')输出的url地址是什么。
这个用firebug很容易就看出 ...
页面源代码是
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ci ajax demo</title>
<script type="text/javascript" src="http://127.0.0.1:81/admincp/default/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
$("#login_submit").click(function(){
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
type: 'post',
//dataType: 'json',
//cache: false,
//url: " http://127.0.0.1:81/admincp/default/test_types_ajax.php",
url: "http://127.0.0.1:81/admin/test/post_action",
data: 'username=' + username + '&password=' + password,
success:
function(msg){
alert("ok");
$("#form_message").html(msg.message).css({"background-color": msg.bg_color}).fadeIn('slow');
},
error: function() {alert("ajax error");}
});
return false;
});
});
</script>
<style type="text/css">
body{
background-color: #fff;
margin: 40px;
font-family:Arial, Helvetica, sans-serif;
font-size: 14px;
color: #4f5155;
}
a{
color: #003399;
background-color:transparent;
font-weight:normal;
}
h1{
color: #444;
background-color:transparent;
border-bottom:1px solid #d0d0d0;
font-size:16px;
font-weight:bold;
margin: 24px 0 2px 0;
padding: 5px 0 6px 0;
}
code{
font-family:Monaco, Verdana, Arial;
font-size:12px;
background-color:#f9f9f9;
border:1px solid #d0d0d0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#form_message{
display:none;
text-align:center;
margin-bottom:5px;
padding:10px;
}
</style>
</head>
<body>
<h1>This is the demo page ofr the simple AJAX post in CodeIgniter Using jQuery</h1>
<p>Username: stamhe | Password: 123456</p>
<p>
Try to enter nothing in the fields and click the submit button.<br/>
Try to mismatch the username and/or password and click the submit button.
</p>
<div style="text-align:right; width:500px;">
<div id="form_message"></div>
<form name="ajax_form" id="ajax_form" method="post">
Username/Email:*<input type="text" name="username" id="username" size="30" /><br/><br/>
Password:*<input type="password" name="password" id="password" size="30" /><br/><br/>
<input type="submit" value="Submit" name="login_submit" id="login_submit" />
</form>
</div>
</body>
</html>
我看地址没有问题,我找不到原因。 没看明白 sevenk 发表于 2012-5-4 17:05 static/image/common/back.gif
页面源代码是
把ajax方法里的data去掉再看呢?
或者是控制器的权限造成的呢? 在控制器里返回JSON的时候使用这个$this->output->set_content_type('application/json')->set_output(json_encode($msg));
我用这个就可以在ajax里面的success:function(msg){} 里面接收到msg了 admin/
admincp/
同一个地址? 404not found,就是url错误呗。用绝对路径试试。一点点测试。
页:
[1]