在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');
我的两分钱.