21

I have a jquery function that calls a PHP file via AJAX. Inside that PHP file I have

 header('Location: http://www.google.com');

However this doesn't work, the page is not redirected and the jQuery AJAX call returns an error, no 12017, I can't find much information about this error number.

I have output buffering enabled and have tried placing the header call right at the top of my PHP file but still have no luck. Any ideas? Thanks.

David Healey
  • 566
  • 5
  • 20
  • Why are you returning a redirect by AJAX? What are you trying to do? – Alexander Feb 17 '13 at 00:20
  • If you view the page in browser. (without AJAX) Is the redirect working? – hek2mgl Feb 17 '13 at 00:21
  • @Alexander: The ajax function posts a form to a PHP function that processes the form, if the form is sucesfully processed then the page should redirect, if it is unsucessful an error is returned to ajax via the ajax success callback and I process the error there. – David Healey Feb 17 '13 at 00:38
  • @hek2mgl I will try this tomorrow and see – David Healey Feb 17 '13 at 00:39
  • I believe you can find your answer here: [How to manage a redirect request after a jQuery Ajax call](http://stackoverflow.com/q/199099/1331430) – Fabrício Matté Feb 17 '13 at 00:45

1 Answers1

9

I believe if you are using ajax request, you have to handle redirects within javascript (where the request is made).

If your php script sets the Location header of the response when you make an ajax request, it will try to redirect the ajax request not the page that is displayed in the browser.

You can try to handle the error where you make the request (I would have been more specific if I saw the code making the request).

You can try changing your script so that it returns some kind of status code and then handle this status with js/jquery and make the redirect there:

document.location=' *new url to redirect the browser to* '
bssstudio
  • 174
  • 3