_remap 忽略 IS_AJAX 调用?

发布于 2024-11-27 19:23:18 字数 1183 浏览 2 评论 0 原文

这个问题很可能是 codeigniter 特有的。

我有一个名为redirect.php 的控制器,它可以从视图重定向到视图。该控制器大部分都有一个公共 _remap 函数,该函数使用 case 语句执行所有重定向。一切都运行良好,直到我将 $.POST 从视图发送回控制器。我希望它能够命中 _remap 并查找请求来自 AJAX 的事实,然后执行此操作。

我正在检查一个 IS_AJAX 常量。

define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

但是每当我点击该页面时,它总是会重新映射到默认值并将我的请求发送到该页面,当我来回回显和提醒数据时,它基本上会返回页面数据。

有什么见解吗?

作为参考,

redirect.php(有更多代码来定义变量和另外两种情况,但它没有命中这些,它命中了“索引”/默认值)

 public function _remap($method)
        {   

    switch ($method) {
        case $method == 'index':
        $this->load->view('main');
            break;
        case $method == 'IS_AJAX':
        var_dump($_POST);
            break;
        default:
        $this->load->view('main');
            }
        }

tweetview.php(在redirect.php中的另一种情况下由重定向控制器加载的视图,json_tweets send 是一个 JSON 变量)

//jquery

$.post("http://localhost/2fb/index.php/redirect", {'json_tweets': json_tweets},
   function(data) {
     alert(data);
   });

This issue is very likely codeigniter specific.

I have a controller called redirect.php that redirects from and to views. This controller for the most part has one public _remap function that does all the redirecting with a case statement. Everything has been working great until I sent a $.POST from a view back to the controller. I want it to hit the _remap and look for the fact that the request is coming from AJAX then do it’s case.

I have a IS_AJAX constant I’m checking against.

define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

but whenever I hit the page it’s always remapping to the default and sending my request to that page where it’s basically returning me that pages data back when I’m echoing and alerting the data to and fro.

Any insights?

for reference,

redirect.php (there is more code to define variables and 2 more cases but it's not hitting those, it's hitting 'index' / default)

 public function _remap($method)
        {   

    switch ($method) {
        case $method == 'index':
        $this->load->view('main');
            break;
        case $method == 'IS_AJAX':
        var_dump($_POST);
            break;
        default:
        $this->load->view('main');
            }
        }

tweetview.php (view loaded by redirect controller in another case within redirect.php, json_tweets send is a JSON variable)

//jquery

$.post("http://localhost/2fb/index.php/redirect", {'json_tweets': json_tweets},
   function(data) {
     alert(data);
   });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女中豪杰 2024-12-04 19:23:18

您可以依赖 $this->input->is_ajax_request() 而不是执行所有这些操作rel="nofollow">http://codeigniter.com/user_guide/libraries/input.html。如果您对加载库不感兴趣,这里有一些类似的代码,我至少在过去两年中已经在生产中使用过。

$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest')? true : false;

看一下字符串,它的 XMLHttpRequest 和 jQuery 是前端 JS 工具包

只是为了添加更多内容,我通常有一个 Ajax 调用的入口点,所以显然我有一个用于 ajax 的控制器并路由我的所有调用通过它。我为什么要这么做?原因很简单,如果 JS 关闭,我确保所有表单都提交到服务器上的简单非 ajax 表单处理程序。如果 JS 打开,jQuery/prototype/YUI 就会控制并将数据发送到我的 ajax 控制器。实际上执行所有验证/验证/数据库交互的结束处理程序是通用代码。

Instead of doing all this you can rely on $this->input->is_ajax_request() from http://codeigniter.com/user_guide/libraries/input.html. If you are not interested in loading a library, here is somewhat similar code I have in production for last two years at least.

$ajax = ($_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest')? true : false;

Look at the string, its XMLHttpRequest and the jQuery is the frontend JS toolkit

Just to add more, I usually have one entry point for Ajax calls, so obviously I have one controller for ajax and route all my calls through it. Why would I do that? Simple reason, I make sure all my forms submit to a simple non ajax form handler at server if JS is turned off. If JS is on, jQuery/prototype/YUI takes control and sends data to my ajax controller. The end handler which actually does all the validation/verification/db interaction is common code.

千纸鹤带着心事 2024-12-04 19:23:18
case $method == 'IS_AJAX':

您的 $method 不是具有此网址的 IS_AJAX

http://localhost/2fb/index.php/redirect

这会将您带到没有方法的 redirect 控制器(默认为“index”) 。您确实需要:

http://localhost/2fb/index.php/redirect/IS_AJAX

...进入该案例。您似乎将您的常量 IS_AJAX 与请求的方法混淆了,您在检查 index 时似乎正确使用了该方法(尽管这是与 default 情况相同,因此是多余的)。

$method,或者您在 _remap() 中命名的第一个参数,将始终是被调用的路由控制器函数。

编辑:我之前没有提到这一点,但是 switch 块会评估您传递给它的表达式,因此无需手动进行比较。例子:

switch ($method) {
    // case $method === 'index':
    case 'index':
        $this->load->view('main');
    break;
}
case $method == 'IS_AJAX':

Your $method is not IS_AJAX with this url:

http://localhost/2fb/index.php/redirect

This would bring you to the redirect controller without a method (will default to "index"). You literally would need:

http://localhost/2fb/index.php/redirect/IS_AJAX

...to step into that case. You seem to be confusing your constant IS_AJAX with the method requested, which you seem to be using correctly when checking for index (although this is the same as the default case, so it's redundant).

$method, or whatever you name the first parameter in _remap(), will always be the routed controller function that is called.

EDIT: I failed to mention this earlier, but the switch block evaluates the expression you pass to it, so there is no need to do the comparison manually. Example:

switch ($method) {
    // case $method === 'index':
    case 'index':
        $this->load->view('main');
    break;
}
迎风吟唱 2024-12-04 19:23:18

试试这个,

    $.ajax({
        type: "POST",
        url: "your function url goes here",
        data: data,
        success: function(html){
              window.location = 'redirect url goes here';
        }
   });

try this ,

    $.ajax({
        type: "POST",
        url: "your function url goes here",
        data: data,
        success: function(html){
              window.location = 'redirect url goes here';
        }
   });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文