views:

145

answers:

3

I am building a page that may receive LARGE amounts of html from an ajax call, which it than insert into the page. Sometimes, instead of the html i am expecting, i will get an http 401 error. Ideally i want to have the error handler fire first, so i can set an error flag, display a message, and NOT insert the html that comes in.

The problem i am having is that the error handler fires LAST, and so i have already inserted the html, which contains unexected script tags, and it makes everything blow up.

also, i noticed that ajaxError fires after the 'complete' handler, which blows.

+3  A: 

You should be using the success handler instead of complete to update the html because it will be triggered only if the server sends 200.

Darin Dimitrov
you are right, thanks for pointing out the fault in my thinking. i was expecting a different status code. a quick check showed me that i was indeed getting a 200, which makes this whole question moot
mkoryak
A: 

If you are using a GET Method instace POST in the ajax call, the problem is the limit o characters you can receive.

One solution colud be, save previously the content in a temporal xml o similar, and take the content after de call.

A: 

A couple of thoughts:

  • Setup your ajax callback function so that it checks the content of the returned string to make sure it is valid. Do not insert it if it is not what you are expecting.

  • You ajax error handler should be not executing the callback (ie, inserting the HTML) AND executing the error handling code. You may have an error elsewhere if this is occuring.

  • You may want to look at setting the timeout option, if needed.
jonstjohn