这是我的文件夹结构:
RewriteEngine on
# redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Allow from all
# disable directory browsing
Options All -Indexes
这是我的AdController.js代码:
$(document).ready(function()
{
$("body").on("change click keypress", "select#state",function()
{
var state_id = $("#state").val();
var host = window.location.hostname;
if(state_id != "")
{
$.ajax({
url: '/manage/modules/Controllers/ControllerRequestHandlers/AddRequestHandler.php?id=getCity',
type: 'POST',
dataType: 'html',
data: {state_id : state_id},
})
.done(function(resp)
{
$("#city").html("<option value='' selected disabled>Select State To List Cities</option>");
$("#city").append(resp);
})
.fail(function()
{
console.log("error");
});
}
});
});
这是我的AddRequestHandler.php代码:
<?php
if(isset($_SESSION['user_id']) AND !empty($_SESSION['user_id']) AND isset($_SESSION['role']) AND !empty($_SESSION['role']))
{
if(extract($_POST) > 0)
{
$id = $_GET['id'];
switch($id)
{
case 'getCity':
echo $add->listCities($_POST['state_id']);
break;
default:
// echo "None";
break;
}
}
}
else
{
header("Location: ../../index.php");
}
?>
这是我的前端页面代码:(create_city.php - 出现在模块文件夹中)
<?php
if(isset($_SESSION['user_id']) AND !empty($_SESSION['user_id']) AND isset($_SESSION['role']) AND !empty($_SESSION['role']))
{
?>
<!DOCTYPE html>
<html>
<head>
<?php
include("/modules/Controllers/AddController.php");
include("title_meta_css.php");
?>
</head>
<body class="skin-blue sidebar-mini">
<div class="wrapper">
<?php include("header.php"); ?>
<!-- Left side column. contains the logo and sidebar -->
<?php include("sidebar.php"); ?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Control Panel
<small>Create City</small>
</h1>
<ol class="breadcrumb">
<li class="active"><a href="dashboard">Dashboard</a></li>
<li class="active">Create City</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<!-- general form elements -->
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">Create New City</h3>
</div><!-- /.box-header -->
<!-- form start -->
<form role="form">
<div class="box-body">
<div class="form-group col-xs-12 col-md-6">
<label>Select State To List Cities</label>
<select required id="state" class="form-control">
<option value="" selected disabled>Select State</option>
<?php $add->listStates(); ?>
</select>
</div>
<div class="form-group col-xs-12 col-md-6">
<label>Select City</label>
<select required id="city" class="form-control">
<option value="" selected disabled>Select State To List Cities</option>
</select>
</div>
<div class="checkbox text-justify col-xs-12 col-md-12">
<label>
<input id="user-status" type="checkbox"> Click the checkbox to set the city <b>Active</b>, else leave it unchecked to be deactivated and activate it later.
</label>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" id="user-create" class="col-xs-12 col-md-4 col-md-offset-4 btn btn-success">Create City</button>
</div>
</form>
</div>
</section><!-- /.content -->
</div><!-- /.content-wrapper -->
<?php
include("footer.php");
//include("right-sidebar.php");
?>
</div><!-- ./wrapper -->
<?php include("scripts.php"); ?>
<script src="<?php echo $site_url; ?>modules/Controllers/ControllerScripts/AddController.js"></script>
</body>
</html>
<?php
}
else
{
header("Location: ../index.php");
}
?>
在create_city.php中,$ site_url变量保存值 www.vendor.com/manage / 其中www.vendor.com是我在WAMP服务器中创建的虚拟主机
问题是每当我从create_city.php页面发出ajax请求时,它会在下面的快照中显示错误
当我在AddController.js中提供正确的路径时,为什么它在模块文件夹中停止控制,它也默认搜索文件索引
我不知道为什么会这样。请任何人帮我解决这个错误..
答案 0 :(得分:1)
if(isset($_SESSION['user_id']) AND !empty($_SESSION['user_id']) AND isset($_SESSION['role']) AND !empty($_SESSION['role']))
这是罪魁祸首,你的ajax调用没有传递会话参数user_id和role。
在使用ajax之前,总是尝试使用普通的php会话进行测试。如果您不确定,请填写表格。
ajax调用只是发送$_POST['state_id'] = 'state_id'
;
如果条件永远不会满足/使用它,那就没有别的了。
会话在服务器端维护。为了验证用户角色,您必须始终使用可能使用MD5和存储在浏览器中的cookie来发送is_login检查的ID。
您将能够发送这些cookie值进行验证。 Ajax只支持GET或POST,可以通过php获得$ _GET或$ _POST
你不能在PHP中使用带有ajax的$ _SESSION,如前所述它仅在服务器上维护。
你可以做这样的事情
当用户
时 登录模型/控制器中的
<?php
session_start();
if(login_success){
$_SESSION['user_id'] = 'what users name'
$_SESSION['role'] = 'role'
}
如果(注销) {clear session} ?&GT;
答案 1 :(得分:0)
用户被重定向到AddRequestHandler.php的index.php文件。似乎ajax调用没有将会话cookie发送到服务器。
会话cookie应在ajax调用时设置。用户是否正确登录了您的应用程序?您还可以检查是否从FireBug发送会话cookie。请看这个链接:Why is jquery's .ajax() method not sending my session cookie?