为什么Ajax请求在默认情况下搜索目录中的index.php文件而不是指定的路径?

为什么Ajax请求在默认情况下搜索目录中的index.php文件而不是指定的路径?,php,jquery,ajax,Php,Jquery,Ajax,这是我的文件夹结构: 这是我的.htaccess代码: 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 %{REQ

这是我的文件夹结构:

这是我的.htaccess代码:

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代码:

这是我的AddRequestHandler.php代码:

这是我的前端页面代码:create_city.php-出现在modules文件夹中

在create_city.php中,$site_url变量包含值www.vendor.com/manage/ 其中www.vendor.com是在WAMP服务器中创建的虚拟主机

问题是,每当我从create_city.php页面发出ajax请求时,它都会在下面的快照中显示错误

当我在AddController.js中提供正确的路径时,为什么它会停止modules文件夹中的控件,而默认情况下它也是在搜索文件索引


我不知道为什么会这样。请任何人帮我解决这个错误。

用户正在从AddRequestHandler.php重定向到index.php文件。ajax调用似乎没有将会话cookie发送到服务器

会话cookie应该在进行ajax调用时设置。用户是否正确登录到您的应用程序?。您还可以检查会话cookie是否由FireBug发送。请参阅此链接:

这就是罪魁祸首,您的ajax调用没有传递会话参数user_id和role

在使用ajax之前,请尝试使用普通php会话进行测试。如果您不确定,请填写表格

ajax调用只是发送$u POST['state\u id']='state\u id'; 除此之外,如果条件永远不会满足/适用于此

会话在服务器端维护。为了验证用户角色和ID,您必须始终使用MD5和存储在浏览器中的Cookie发送is_登录检查

您将能够发送这些cookie值以进行验证。Ajax只支持php以$\u GET或$\u POST形式获取的GET或POST

您不能像前面所说的那样在PHP中将$\会话与ajax一起使用,它只在服务器上维护

你可以这样做

当用户

在登录模型/控制器中

<?php

session_start();
if(login_success){
$_SESSION['user_id'] = 'what users name'
$_SESSION['role'] = 'role'
}
iflogout {清除会话}
?>

但我使用了启动会话;那么为什么呢?Ohk现在理解了这个概念,谢谢:但是我使用了另一种技术,我改变了文件夹结构,一切都很好。请求似乎没有那么高,因为我调用了脚本,还维护了会话。其他ajax调用在这一次运行得很好,但无论如何,感谢您向我解释了这个概念:
<?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");
        }
?>
<?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");
      }    
?>
if(isset($_SESSION['user_id']) AND !empty($_SESSION['user_id']) AND isset($_SESSION['role']) AND !empty($_SESSION['role']))
<?php

session_start();
if(login_success){
$_SESSION['user_id'] = 'what users name'
$_SESSION['role'] = 'role'
}