我有.htaccess的这些AJAX POST问题。 。在.htaccess中添加一些规则后,似乎AJAX不响应php文件,即.php remove并在.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]
有人可以帮我解决这个问题吗?感谢。
答案 0 :(得分:1)
尝试这样做
$.ajax({
url: 'getDetails/',
type: 'post',
data: {userid:userid,request:2},
dataType: 'json',
success:function(response){
.......
如果您注意到,我会删除getDetails中的.php扩展名。也许是一个小问题。