views:

32

answers:

4

I am populating a dropdown from a jquery ajax call to a web service that is returning json data. If for some reason the webservice fails or is unavailable how do I handle this? I don't want to have my users looking at an empty dropdown.

How can I cache the last successful call and use that instead?

A: 

if the service is unavailable, then you will have to inform your user that the page did not load correctly. or retry once or twice, but chances are if it's down the first time, it'll be down for a while

Joel Martinez
A: 

You can use DOM Storage to cache results in the browsers that support it. Or write a cookie.

And for ones that don't, or if you have no data, just hide the drop down and display an error panel in its place.

jeffamaphone
+1  A: 

If the data is the same for all users and doesn't change too frequently, you could cache it on your server, and only do the AJAX call if cache is unavailable/stale. (If cache is stale, yet AJAX call fails, it may be better to serve slightly stale data than no data.)

Piskvor
+1  A: 

Be careful about caching the data - there are several services (I'm thinking about the DVLA here in Britain) for which caching data constitutes a break of their terms of usage...

You need to indicate to the user that the service has failed for whatever reason.

Perhaps you should try to load the data once the page is ready, and if the webservice fails navigate to an error page?

Martin Milan