views:

40

answers:

2

I have a pagination control that refreshes a DIV via some JQuery Ajax, however if and when an error occurs say if the page crashes because it couldn't connect to the database or some other reason. My default error page gets displayed inside that DIV instead of the results..

So my question is how do you make it so that doesn't happen, that when the error occurs that it re-routes the user to the error page instead of just refreshing the DIV with the contents of the error page?

Update: I am just doing a simple

$("#PagerContainer").load($(this).attr("href"));

On a link click event handler.

What I need it to do is do full redirect to the error page instead of putting the contents into the DIV

The error is trapped from the Global.Aspx from the Application_Error

Another Update: Where this is happening isn't actually an ajax post it is a normal form submit from with in a modal... What would I do in that case.

A: 

Look at the documentation for JQuery.ajax(), which has support for doing different things depending on the status code of the request.

However, you might need to do some checking in your controller action for ajax calls too - there's a property (well, actuallly a method that returns boolean...) on the Request object called IsAjaxRequest() or something, that can help you return different things depending on if the request was made with ajax or not.

Tomas Lycken
I am just doing a simple $("#PagerContainer").load($(this).attr("href"));
dswatik
Yeah. Well, `$.load()` is a shortcut to a call to `$.ajax()` which only has the handling of a successful result specified. Try switching it out (if the status code of your error page is something other than 200), or go with the controller approach. (That could mean you need to check for ajax call in your error handling too...)
Tomas Lycken
I'm not quite sure if you're catching what my problem is... The problem is I need to make it so the contents of the DIV don't get populated by my error page, but instead it does basically a total browser redirect to the error page.
dswatik
Yes, I know. I mean you should use `$.ajax()` instead, to get all the configurability. Have you checked (in Firebug, for example) what the status code of the result is when you get an error? I.e., does the server return the error page as a regular page, or with a modified header to notify that there was a problem? If there is a different header ("status code" != 200) then you can achieve that by having the client script redirect if the status code is incorrect. (This also requires that you know the url of the error page...) An option is to modify your error handling, so that it is ajax-aware.
Tomas Lycken
The error is trapped from the Application_Error, the error isn't a Ajax error it's a MVC ASP.NET application error.
dswatik
That's what I'm talking about. There is build in functionality in `$.ajax()` to handle **server side** errors, depending on the status code of the response from the server.
Tomas Lycken
+1  A: 

Instead of the code you posted do something like

var theUrl = $(this).attr("href");

$.ajax({
  type: "GET", 
  url: theUrl,
  success: function(data) { $("#PagerContainer").html(data); /*This will insert the HTML returned from the server */  },
  error: function(data) { alert('I encountered an error. I will NOT insert any HTML'); } 
}); 

As you can probably guess, the function named 'success' will be called if a positive response is received from the server (HTTP Status 200). If an error code is received, the function named 'error' will be executed.

Rune