2017-09-13 75 views
0

我有這些與.htaccess的AJAX POST問題。 。在.htaccess中添加一些規則後,AJAX將不響應php文件,這是.php在.php文件末尾刪除和添加斜線。以下是我的詳細代碼。AJAX將不響應POST php在.htaccess

我有這些mysql數據庫

enter image description here

與AJAX這些的index.php旁邊張貼,並要求自動完成getDetails.php到我的文本字段。直到我在目錄中添加的.htaccess代碼

的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來刪除.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] 

有人可以幫我解決這個問題呢?謝謝。

回答

1

嘗試這樣做

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

如果你注意到了,我刪除getDetails php擴展。也許是一個小問題。