mkoryak
mkoryak

Reputation: 57968

jquery: ajaxError always fires last

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.

Upvotes: 0

Views: 345

Answers (3)

jonstjohn
jonstjohn

Reputation: 60286

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.

Upvotes: 0

Viure
Viure

Reputation:

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.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039100

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.

Upvotes: 4

Related Questions