如何在 .htaccess 重定向后通过 ajax 请求保留 POST 数据? [英] How to preserve POST data via ajax request after a .htaccess redirect?

查看:23
本文介绍了如何在 .htaccess 重定向后通过 ajax 请求保留 POST 数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.htacesss

RewriteCond %{REQUEST_URI} ^/api/(.+)$
RewriteRule ^api/(.+)$ /index.php?api=%1 [QSA,L]

示例 ajax url 请求:'http://hostname.com/api/ext/list.php?query=de'

example ajax url request: 'http://hostname.com/api/ext/list.php?query=de'

我希望能够将这种格式的网址重定向到以下内容index.php?api={requested_filename}&param1=value1&param2=value2 ...

I want to be able to redirect urls in this format to the following index.php?api={requested_filename}&param1=value1&param2=value2 ...

因为整个站点是通过 index.php 中的引导程序处理的,其中有一个路由部分加载配置、模板等...

because the whole site is processed through a bootstrap process in index.php which has a routing part loading configs, templates etc...

例如,当我尝试使用 jquery 代码时,重定向后 POST 数据丢失.

When I try a jquery code for example, the POST data is lost after redirect.

$.ajax({
            url: '/api/contact.php',
            type: 'POST',
            data: { 
                email: $("#contactEmail").val(),
                name: $("#contactName").val(),
                message: $("#contactMessage").val()
                                // etc ...
            }
});

我了解到您无法在 http 重定向中保留数据.但是所有的框架如何避免这种情况呢?我已经编写了很多代码,并且每个代码都是通过 index.php 引导的,并且在 .htaccess 文件中有重写规则,用于启用漂亮的 url.例如,在 Yii 中,我会使用一些 POST 数据调用 url "api/uploads/latests.json",后端的控制器将接收该数据.我在这里错过了什么?

I've read that you cannot preserve data on a http redirect. But how do all the frameworks avoid that? I've coded in many, and every one is bootstraped through the index.php and there are rewrite rules in the .htaccess file for enabling pretty urls. So in Yii for example, I would call an url "api/uploads/latests.json" with some POST data and the controllers on the backend would receive that data. What am i missing here?

注意:我已经测试了 [P] mod_rewrite 参数,我认为这个服务器没有启用 mod_proxy.

note: I've tested the [P] mod_rewrite parameter, and i think that this server doesn't have mod_proxy enabled.

推荐答案

重写和重定向是有区别的.

There is a difference between a rewrite and a redirect.

Rewrite 是一个 apache(和其他服务器)模块,它将遵循一组 cond/rules 将请求的 url 映射到服务器上的文件(例如:引导程序将所有 url 重写为单个文件,通常是 index.php.mvc 可能将/model/controller/view uri 映射到调用适当 mvc 文件的 index.php).

Rewrite is an apache (and other servers) module that will follow a set of cond/rules to map a requested url to files on the server (ex: a bootstrap rewrites all urls to a single file, usually index.php. A mvc might map /model/controller/view uri to an index.php that calls the appropriate mvc files).

重定向实际上会更改您所在的页面.有人请求页面 A.php,该页面显示您要查找的内容在 B.php 上",因此您的浏览器转到 B.php.

A redirect actually changes the page you are on. Someone requests page A.php and that page says "what you are looking for is on B.php" and so your browser goes to B.php.

重写将保留 post 参数,因为 url 不会改变.重写只会更改被请求的脚本,但在浏览器看来,该页面仍然存在于请求的 url 中.

A rewrite will preserve post parameters because the url doesn't change. A rewrite will just change the script being requested, but to the browser it looks like the page still exists at the requested url.

重定向不会保留帖子参数,因为服务器会将您完全重定向到另一个页面.

A redirect will not preserve post parameters because the server will redirect you to another page completely.

看起来您正在尝试做的是重写,而不是重定向.获取 post 参数应该没有问题.

What it appears you are trying to do is a rewrite, not a redirect. You should have no problems getting the post parameters.

要解决这个问题,您如何检查 index.php 中没有 post 参数?您确定正在调用您期望的控制器吗?

To fix this, how are you checking in index.php that there are no post parameters? Are you sure the controller you are expecting is getting called?

这篇关于如何在 .htaccess 重定向后通过 ajax 请求保留 POST 数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆