Saturday, 16 September 2017

jquery - PHP AJAX 12017 Error when calling Header(Location:)




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.


Answer



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* '

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...