简体   繁体   English

AJAX POST 请求与 .htaccess 重定向规则一起无法正常工作

[英]AJAX POST request does not work properly along with .htaccess redirect rule

This is my .htaccess file.这是我的 .htaccess 文件。 As it can be seen everything is redirected through index.php.可以看出,一切都通过 index.php 重定向。

<IfModule mod_rewrite.c>
  Options -Multiviews
  RewriteEngine On
  RewriteBase /project_name/public
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule  ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>

Now this is a file (viewname.php) located in one of my views folder (this is a MVC styled project).现在这是一个文件(viewname.php),位于我的一个视图文件夹中(这是一个 MVC 风格的项目)。 Here as it can be seen I am making an AJAX POST request, to a file ajax.php located in the same folder as index.php (ie, folder public). Here as it can be seen I am making an AJAX POST request, to a file ajax.php located in the same folder as index.php (ie, folder public). Notice that I'm not writing the url as "../something", as I'm redirecting everything through index.php.请注意,我没有将 url 写为“../something”,因为我正在通过 index.php 重定向所有内容。 I confirmed that by writing <?php echo __FILE__;?> , in this file, which returned the result index.php.我通过在此文件中写入<?php echo __FILE__;?>确认了这一点,该文件返回了结果 index.php。 If the AJAX request is successful, I should get the output as "Working" in the div with id 'r'.如果 AJAX 请求成功,我应该在 id 为“r”的 div 中将 output 设为“正在工作”。 But that does not happen.但这不会发生。 Also I do not get any error like jquery.min.js:4 POST http://127.0.0.1/project_name/public/file_name.php 404 (Not Found) in the console, which I used to get in a project without that .htaccess file. Also I do not get any error like jquery.min.js:4 POST http://127.0.0.1/project_name/public/file_name.php 404 (Not Found) in the console, which I used to get in a project without that .htaccess 文件。 When I console.log(data) I see the HTML code in the index.php file, which in this case are CDNs for Bootstrap and JQuery, in the console.当我console.log(data)我在 index.php 文件中看到 HTML 代码,在这种情况下是引导程序和 JQuery 的 CDN。 What changes should I make to my AJAX code or .htaccess file?我应该对我的 AJAX 代码或 .htaccess 文件进行哪些更改?

<div id="r"></div>

//JQuery AJAX
$(document).ready(function(){
 $.ajax({
  url: "ajax.php",
  method: "POST",
  data: {
   valid: "yes"
  },
  success: function(data){
  $("div#r").html(data);
  }
 });
});

This is the code in the ajax.php file:这是 ajax.php 文件中的代码:

<?php
if(isset($_POST['valid'])){
 if($_POST['valid']=='yes'){
  echo 'Working';
 }
}

you missing function after ()(round bracket)你在()之后缺少 function(圆括号)

$(document).ready(function(){
         $.ajax({
          url: "test1.php",
          method: "POST",
          data: {
           valid: "yes"
          },
          success: function(data){
          $("div#r").html(data);
          }
         });
    });

Here in this case the AJAX works completely but the only difference is that the ajax cannot make a difference to your webpage like echo,call the function,etc.在这种情况下,AJAX 可以完全工作,但唯一的区别是 ajax 不能像 echo 一样对您的网页产生影响,请致电 function 等。

I changed the url from "ajax.php" to " http://127.0.0.1/project_name/ajax.php ", and everything is working fine now.我将 url 从“ajax.php”更改为“ http://127.0.0.1/project_name/ajax.php ”,现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM