在Ajax调用之后是否可以使用PHP重定向页面? [英] Is it possible to redirect the page with PHP after an Ajax call?

查看:61
本文介绍了在Ajax调用之后是否可以使用PHP重定向页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,我想为用户提供一个选项,如果他们单击表的行,他们会重新定位到另一页(基于表行的接触).

I have a website where I want to provide an option for users, if they click on a table's rows, they get regirected to another page (based on the contect of the table row).

所以我要做的基本上是使用jQuery收集数据并将其发布到PHP文件中.在我的PHP文件中,我想使用 header('Location:***').

So what I'm doing is basically collecting the data with jQuery and POSTing that to a PHP file. In my PHP file I'd like to do the redirecting using header('Location: ***').

这是正确的方法吗?因为这样,ajax请求不会得到任何返回,没有成功,没有任何事情,在此之前它会被重定向.

Is it a correct way to do this? Because this way, the ajax request does not gets anything returned, no success, no nothing, it get redirected before that.

那么这是我可以使用的路径,还是我应该提出另一个想法?

So is it a path that I could use, or should I come up with another idea?

推荐答案

是,不是.如果您使用的是jquery,并且您的ajax调用会以这样的json进行响应:

Yes and no. If you work with jquery, and your ajax call responds with a json like this:

{
    success : true, 
    redirect_to : "http://www.google.com"
}

在php中意味着

<?php echo json_encode([
    'success' => true,
    'redirect_to' => 'http://www.google.com',
]);

然后您的ajax调用者可以执行肮脏的工作:

then your ajax caller can do the dirty job:

$.get('/your/path', function (json) {
    if(json.success) {
        document.location.href = json.redirect_to;
    }
}, 'json');

我的两分钱.

这篇关于在Ajax调用之后是否可以使用PHP重定向页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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