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

后端 未结 4 714
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 03:57

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).

4条回答
  •  梦毁少年i
    2021-01-29 04:18

    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. 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)

    If you intend to always redirect, then don't use 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.

提交回复
热议问题