1

I am using jQuery's ajax functions to make my ajax request.

My question is if I caught an error how do I then redirect to a error page, say I've got the error, logged it, and now want to redirect to a error page, is it possible within the ajax request or will it just redirect the request not the page performing the request?

Do I have to state an error has occurred within the request, by changing the headers HTTP status to say 500 then within the ajax error function direct to a error page?

However within my ajax request if I have already echoed out data and then want to change the header information, will cleaning the buffer allow me to then change the headers?

Is this how I would clean the buffer?

if(ob_get_length()) ob_clean();

How I normally redirect, include a error page :

header('HTTP/1.1 500 Internal Server Error', TRUE, 500);
readfile("500.html");

Within my ajax request would I just change the header like above and then deal with the rest with jQuery?

Jack Trowbridge
  • 3,175
  • 9
  • 32
  • 56

2 Answers2

2

its up to you how you want to handle it, you could return a 500, or you could always return a 200 and a status message.

You will have to change page from inside javascript though, in response to your ajax call,

you can do this using location property of window

window.location = '/yourlocation'

another similar question about redirecting in response to an ajax call

Community
  • 1
  • 1
dm03514
  • 54,664
  • 18
  • 108
  • 145
1

You can redirect using javascript , after ajax call , on error :

error: function () {

    // similar behavior as an HTTP redirect
    window.location.replace("http://mysite.com/500.html");

  }
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44