I would like to handle all the errors in a single place.
Is it possible to attach an error handler to the XMLHttpRequest object (or somthing like that), in one place that will handle all the requests?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you had a single single XMLHttpRequest object, you would listen to the
readystatechangeevent to detect, when the operation is complete, and then read thestatusproperty to detect an error.Now, if you want to do this with all objects, you need to listen to all occurring
readystatechangeevents, i.e. register to the respective event, if a XMLHttpRequest is created or initialized. This can be done, by overriding the open-method of the XMLHttpRequest prototype, so that it registers your event listener and then calls the old open.I have used the following code for that (in Firefox):
(this assumes that
openis called only once per XMLHttpRequest object or your event is registered multiple times, if you want to be safe, you could probably reset the open-function by addingthis.open = open;)