I would like to be able to send and receive MessagePack-formatted data with jQuery's $.ajax()
. How does one extend $.ajax()
to support new Content-Type
formats?
Asked
Active
Viewed 1,164 times
4

David Eyk
- 12,171
- 11
- 63
- 103
-
A string is a string, jQuery datatypes only help you by converting the string to object, html, xml etc. So just send the string, and do your own parsing. – adeneo May 06 '13 at 21:45
-
Sure, but it's nice the way jQuery automatically parses `json` responses for me. If I can extend jQuery's content-type recognition, then I could potentially switch a project from `application/json` to `application/msgpack` without peppering the codebase with calls to the parser. – David Eyk May 06 '13 at 21:48
-
You can just write your own reusable function to do that, without extending $.ajax, but you could of course extend the $.ajax function if that's what you want, but it will be more complicated than writing a new ajax function that uses $.ajax internally, and parses and returns the results for you. – adeneo May 06 '13 at 21:50
-
There's a method `$.ajaxPrefilter()` that looks like it does what you want. – Barmar May 06 '13 at 21:51
1 Answers
0
Use $.ajaxPrefilter(), which allows you to define custom processing before the AJAX request is sent.
To process custom types of returned data, use the converters
option to $.ajax()
or $.ajaxSetup()
.

Barmar
- 741,623
- 53
- 500
- 612
-
It's not immediately obvious from the docs how to use these. `$.ajaxPrefilter()` handles request processing, while `converters` handles response processing? – David Eyk May 06 '13 at 22:14
-
Yeah, the documentation isn't very good on this. Your best bet is to read the jQuery source code to see how it's used internally for the predefined types. – Barmar May 06 '13 at 22:16