0
votes

I know its a basic question and it is been asked for several times but i was not able to understand it.

I was using a html webpage with some input fields and submit button when the submit button is pressed the post is done through XMLHttpRequest and CGI script is called. In cgi script the authentication is checked with the value in the file of server.

The problem is that if the authenication is false i want to redirect the browser to the xmltest.shtml for this i have written in the CGI:

 if($isauthenticated == 0)
   {
     print "Location: http://xmltest.shtml\n\n";
     }

But when this cgi is called in return the get is called with the xmltest.shtml page but the browser is not redirected.

It means that if I check in the Firebug console the get request is seen by me for the xmltest.shtml but the browser page is not redirected to the xmltest.shtml it remains to the same page.

1

1 Answers

4
votes

You can't cause the page to redirect that way. When you use XMLHttpRequest, you are sending the redirect header to the XMLHttpRequest client, which runs in the background. You will successfully redirect that client, but it will not affect the page on the screen.

If you want to redirect the actual browser page in response to an XMLHttpRequest session, you will need to write some JavaScript to capture the error condition and redirect the browser by updating the value of document.location.href.

If you're using an AJAX framework like jQuery, there is an error callback available in the ajax method which will get executed if your failed request returns an HTTP 403 or similar.