简体   繁体   English

是否可以在 Ajax 调用后使用 PHP 重定向页面?

[英]Is it possible to redirect the page with PHP after an Ajax call?

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).我有一个网站,我想为用户提供一个选项,如果他们点击表格的行,他们会被重新引导到另一个页面(基于表格行的内容)。

So what I'm doing is basically collecting the data with jQuery and POSTing that to a PHP file.所以我所做的基本上是使用 jQuery 收集数据并将其发布到 PHP 文件中。 In my PHP file I'd like to do the redirecting using header('Location: ***') .在我的 PHP 文件中,我想使用header('Location: ***')进行重定向。

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.因为这样,ajax 请求不会返回任何东西,没有成功,没有任何东西,它在此之前被重定向。

So is it a path that I could use, or should I come up with another idea?那么这是我可以使用的路径,还是我应该想出另一个想法?

No.不。

A redirect tells the client that the resource they just requested can be found elsewhere.重定向告诉客户端他们刚刚请求的资源可以在其他地方找到。

The page that was previously loaded which contains that JavaScript is not the resource that the Ajax request is asking for.先前加载的包含该 JavaScript 的页面不是 Ajax 请求请求的资源。 Something will be returned though—it should get the resource that you redirected to (which will be available in xhr.responseText (assuming the same origin policy doesn't interfere and that you get a 200 OK response at the place you are redirecting to)但是会返回一些东西 - 它应该获得您重定向到的资源(这将在xhr.responseText可用(假设同源策略不会干扰并且您在重定向到的位置获得200 OK响应)

If you intend to always redirect, then don't use Ajax.如果您打算始终重定向,则不要使用 Ajax。

If you intend to sometimes redirect, then you'll need to put the URL in the body of the response, parse it with JavaScript and then assign it to location .如果您打算有时重定向,那么您需要将 URL 放在响应正文中,使用 JavaScript 对其进行解析,然后将其分配给location

You cannot redirect with PHP, however you can easily redirect with the response of your ajax call.您不能使用 PHP 重定向,但是您可以使用 ajax 调用的响应轻松重定向。

refer to: window.location.replace(...)参考: window.location.replace(...)

Yes and no.是和否。 If you work with jquery, and your ajax call responds with a json like this:如果您使用 jquery,并且您的 ajax 调用以这样的 json 响应:

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

that in php means在 php 中的意思是

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

then your ajax caller can do the dirty job:那么你的 ajax 调用者就可以完成肮脏的工作:

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

My two cents.我的两分钱。

I basically want to get the id from the row and when the visitor clicks on the row, they should land on example.php?id=XXXXX我基本上想从行中获取 id,当访问者点击行时,他们应该登陆 example.php?id=XXXXX

You can do so entirely from PHP.您可以完全从 PHP 执行此操作。 When you generate the table, in each row (whose id we'll call XXXX to follow your example) you can include a link to example.php?id=XXXXX , or an onclick event for the whole row that executes window.location.href = 'example.php?id=XXXXX';当您生成表格时,在每一行(我们将其 ID 称为XXXX以遵循您的示例)中,您可以包含一个指向example.php?id=XXXXX的链接,或执行window.location.href = 'example.php?id=XXXXX';的整行的onclick事件window.location.href = 'example.php?id=XXXXX'; . . So your table might look like:所以你的表可能看起来像:

...
<tr onclick="window.location.href = 'example.php?id=1234';">
  <td>1234</td><td>some value</td><td>some other value<td>
</tr>
<tr onclick="window.location.href = 'example.php?id=1235';">
  <td>1235</td><td>some value</td><td>some other value<td>
</tr>
...    

It could be done a little nicer by generating the rows with javascript and using event listeners, but this should work.通过使用 javascript 生成行并使用事件侦听器可以做得更好一些,但这应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM