【问题标题】:AJAX won't response to POST php in .htaccessAJAX 不会响应 .htaccess 中的 POST php
【发布时间】:2017-09-13 01:09:37
【问题描述】:

我对 .htaccess 有这些 AJAX POST 问题。 .在我在 .htaccess 中添加一些规则后,AJAX 似乎不会响应 php 文件,即 .php 删除并在 .php 文件的末尾添加斜杠。以下是我的详细信息代码。

我有这些mysql数据库

我将这些 index.php 与 AJAX 一起发布并请求将 getDetails.php 中的自动完成功能添加到我的文本字段中。

index.php

<!doctype html>
<html>
<head>
    <title>AJAX POST</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href='jquery-ui.min.css' type='text/css' rel='stylesheet' >
    <script src="jquery-3.2.1.min.js" type="text/javascript"></script>
    <script src="jquery-ui.min.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function(){
            $(document).on('keydown', '.username', function() {

                var id = this.id;
                var splitid = id.split('_');
                var index = splitid[1];

                $( '#'+id ).autocomplete({
                    source: function( request, response ) {
                        $.ajax({
                            url: "getDetails.php",
                            type: 'post',
                            dataType: "json",
                            data: {
                                search: request.term,request:1
                            },
                            success: function( data ) {
                                response( data );
                            }
                        });
                    },
                    select: function (event, ui) {
                        $(this).val(ui.item.label);  
                        var userid = ui.item.value;  
                        $.ajax({
                            url: 'getDetails.php',
                            type: 'post',
                            data: {userid:userid,request:2},
                            dataType: 'json',
                            success:function(response){
                                var len = response.length;
                                if(len > 0){
                                    var id = response[0]['id'];
                                    var name = response[0]['name'];
                                    var email = response[0]['email'];
                                    document.getElementById('name_'+index).value = name;
                                    document.getElementById('age_'+index).value = age;                                    
                                }

                            }
                        });

                        return false;
                    }
                });
            });
        });

    </script>
</head>
<body>
    <div class="container">
        <table border='1' style='border-collapse: collapse;'>
            <thead>
            <tr>
                <th>Username</th>
                <th>Name</th>
                <th>Age</th>
            </tr>
            </thead>
            <tbody>
            <tr class='tr_input'>
                <td><input type='text' class='username' id='username_1' placeholder='Enter username'></td>
                <td><input type='text' class='name' id='name_1' ></td>
                <td><input type='text' class='age' id='age_1' ></td>
            </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

getDetails.php

<?php
$host = "";  
$user = "";  
$password = "";  
$dbname = "";  
$con = mysqli_connect($host, $user, $password,$dbname);
if (!$con) { die("Connection failed: " . mysqli_connect_error());}

$request = $_POST['request'];
if($request == 1){
    $search = $_POST['search'];
    $query = "SELECT * FROM users WHERE username like'%".$search."%'";
    $result = mysqli_query($con,$query);
    while($row = mysqli_fetch_array($result) ){
        $response[] = array("value"=>$row['id'],"label"=>$row['username']);
    }
    echo json_encode($response);
    exit;
}
// Get details
if($request == 2){
    $userid = $_POST['userid'];
    $sql = "SELECT * FROM users WHERE id=".$userid;
    $result = mysqli_query($con,$sql);
    $users_arr = array();
    while( $row = mysqli_fetch_array($result) ){
        $userid = $row['id'];
        $fullname = $row['fname']." ".$row['lname'];
        $email = $row['email'];
        $users_arr[] = array("id" => $userid, "name" => $fullname,"email" => $email);
    }
    // encoding array to json format
    echo json_encode($users_arr);
    exit;
}
?>

在我在目录中添加 .htaccess 代码之前,一切正常。实际上,我使用这个 .htaccess 删除 .php 扩展名并在文件名末尾添加斜杠 (/)。所以假设test/index.php之类的文件将变为test/index/

Options -Indexes

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /test/$1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ /test/$1 [R=301,L]

有人可以帮我解决这个问题吗?谢谢。

【问题讨论】:

    标签: php jquery mysql ajax .htaccess


    【解决方案1】:

    尝试这样做

     $.ajax({
           url: 'getDetails/',
           type: 'post',
           data: {userid:userid,request:2},
           dataType: 'json',
           success:function(response){
             .......
    

    如果你注意到了,我会删除 getDetails 中的 .php 扩展名。可能是个小问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多