2017-09-13 5 views
0

私はこれらのAJAX POSTに.htaccessの問題があります。 。 .phpファイルの最後に.phpを削除してスラッシュを追加する.htaccessにいくつかのルールを追加したら、AJAXはPHPファイルに応答しないようです。以下は私の詳細なコードです。AJAXは.htaccessのPOST PHPに応答しません

私は私のテキストフィールドにgetDetails.phpでオートコンプリートを投稿して要求するAJAXと一緒にこれらののindex.phpを持っているこれらのMySQLデータベース

enter image description here

を持っています。

の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.phptest/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を削除します。たぶん小さな問題です。

関連する問題