views:

38

answers:

2

Hi, This questions is bit specific to joomla.

I have created the custom error page error.php in /template/MY_TEMPLATE/error.php. This file checks for the error codes and displays different message for each http code I set(I check with $this->error->code == '404'). Normally this works good.

Now the question is, how should I handle same thing when I am using ajax? When I do an ajax call if an error occurs, I would get http status code back, and the response is the error.php file output. When I get error I would like to redirect the user to the error.php (custom error page) with proper error codes, which would display the messages accordingly!

Scenario : - I am requesting an ajax call to a component with a task (com_greatsuff), which executes some webservice calls to get list of stuff based on itemcode provided. If the itemcode is not found on webservice it woud return 404 Error.(There are many other custom error codes). If it was not an ajax call, it works good as i raise error(JError::raiseError), and displays custom error page. But on ajax call, it will return http status(404) but i need to redirect from javascript with the proper http header. Thanks, Tanmay

A: 

This may help:

Sarfraz
@Sarfraz, thanks for reply. I have already created the custom error page, which works good with 404 or any other error code. But I am unware of applying the redirect for the ajax calls. Let also update the question with the scenario I am facing.
jtanmay
@Sarfraz Can you please guide with my implementation below.Thanks
jtanmay
@jtanmay: Your ajax code seems to be fine, what is the problem/issue?
Sarfraz
@Sarfraz: Thanks for your reply, the ajax code is working fine, and does display a error page. I was just concerned if I was doing something wrong, or If there's any better way. But Thanks for comment. I will mark my own answer as accepted.
jtanmay
A: 

Ok, after thinking of various logics, I applied the following. Some one might be helped.. or let me know if any one has a better solution.

  • My main purpose was to show the user the custom error page based on the error codes (404, 403, 500... )
  • Now when ajax fails and returns back with the specific error code (404, 403, 500..) it also returns me the complete custom error page, as discussed in my question.
  • I grabbed the complete HTML page and replaced the current page with it.

eg:

$.ajax({
  url: 'index.php?option=com_test&task=listdata&no_html=1',
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  }
  failure: function(data){
     document.write("");
     document.close();
     document.write(data.responseText);
     document.close();
}
});

This will basically overwrite the current page with the custom error page generated through joomla.

Please let me know if I am doing anything wrong.

Thanks, Tanmay

jtanmay
@anyone can I please get some guidance!! Thanks
jtanmay