How to extend jquery jqXHR object

Can we add additional methods to the jqXHR object? I had an application built using prototype.js that extended the answer

Ajax.Response.addMethods({}); 

What is equivalent to jquery?

I want to add some methods specific to my application. For example, I want to add a method

 getMyHeader: function(name) { return this.getResponseHeader('MY-'+name); } 

which returns me the specific header sent by the server.

+4
source share
1 answer

It doesn't seem like jQuery specifically supports this. The jqXHR object they create is just a universal object with methods added as properties, so there is no prototype that you can change, and I don't see any jQuery methods to modify it.

The only thing I could see was to register the ajax global event handler, for example jQuery.ajaxSend() , and add the necessary methods for each specific jqXHR object in this global handler. Then these methods will be available later.

+1
source

Source: https://habr.com/ru/post/1432615/


All Articles