【问题标题】:mod_rewrite do not work with jQuery.ajaxmod_rewrite 不适用于 jQuery.ajax
【发布时间】:2014-03-01 21:09:52
【问题描述】:

我正在使用 WAMP、jQuery 和 CodeIgniter。我已经完成创建 .htaccess 以删除 index.php 并且它可以正常工作。

但是使用 $.ajax 是行不通的。它必须通过 index.php 访问。我究竟做错了什么? ?

这里是 .htaccess 文件

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

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

  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

这里是调用者

var url =  base_url + 'spider/ajax_trade_open/';

//var url =  base_url + 'index.php/spider/ajax_trade_open/'; // this work

$.ajax({
  'url': url,
  'type': 'GET',
  'success': function (data) {
    alert('success');
  },
  'error': function (obj, title, ex) {
    alert(title + ': ' + ex);
  }
});

谢谢。

【问题讨论】:

  • 在这条规则中 RewriteRule ^(.*)$ index.php?/$1 [L] 我认为您需要将 index.php?/$1 替换为 index.php/$1

标签: php jquery ajax .htaccess mod-rewrite


【解决方案1】:

在现有规则之前添加此规则:

RewriteRule ^(index\.php/(.+)$ $1?/$2 [L,NC,QSA]

【讨论】:

  • 这是根据您的评论调整后的 htaccess。我做错了什么?
  • RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(index\.php/(.+)$ $1?/ $2 [L,NC,QSA] RewriteRule ^(.*)$ index.php?/$1 [L]
  • 对于哪个 URL 不起作用,您是否收到 404?
  • 网址是“localhost/spider/ajax_trade_open”。除了通过 $.ajax 调用外,一切正常。它返回带有 Not Found 描述的错误状态。使用 index.php 它可以工作。非常感谢您的支持。
  • 对不起我的英语不好。它不适用于 AJAX。要使用 $.ajax() 调用,我需要使用 index.php 设置 url。这让我想知道,直接访问调用和 $.ajax() 的 GET 方法有什么区别。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 2013-11-09
  • 1970-01-01
相关资源
最近更新 更多