Skip to content

Commit de767fe

Browse files
addyosmanikswedberg
authored andcommitted
Adding section on using $.ajax() converters with custom data types. Fixes bug ticket 11376
This addresses: http://bugs.jquery.com/ticket/11376
1 parent 7029225 commit de767fe

1 file changed

Lines changed: 59 additions & 18 deletions

File tree

entries/jQuery.ajax.xml

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -261,27 +261,68 @@ jqxhr.always(function() { alert("second complete"); });</pre>
261261
<pre>
262262
var _super = jQuery.ajaxSettings.xhr;
263263
jQuery.ajaxSettings.xhr = function () {
264-
var xhr = _super(),
265-
getAllResponseHeaders = xhr.getAllResponseHeaders;
266-
267-
xhr.getAllResponseHeaders = function () {
268-
if ( getAllResponseHeaders() ) {
269-
return getAllResponseHeaders();
270-
}
271-
var allHeaders = "";
272-
$( ["Cache-Control", "Content-Language", "Content-Type",
273-
"Expires", "Last-Modified", "Pragma"] ).each(function (i, header_name) {
274-
275-
if ( xhr.getResponseHeader( header_name ) ) {
276-
allHeaders += header_name + ": " + xhr.getResponseHeader( header_name ) + "\n";
277-
}
278-
return allHeaders;
279-
});
280-
};
281-
return xhr;
264+
var xhr = _super(),
265+
getAllResponseHeaders = xhr.getAllResponseHeaders;
266+
267+
xhr.getAllResponseHeaders = function () {
268+
if ( getAllResponseHeaders() ) {
269+
return getAllResponseHeaders();
270+
}
271+
var allHeaders = "";
272+
273+
$( ["Cache-Control", "Content-Language", "Content-Type",
274+
"Expires", "Last-Modified", "Pragma"] )
275+
.each(function (i, header_name) {
276+
if ( xhr.getResponseHeader( header_name ) ) {
277+
allHeaders += header_name + ": " + xhr.getResponseHeader( header_name ) + "\n";
278+
}
279+
return allHeaders;
280+
});
281+
};
282+
283+
return xhr;
282284
};
283285
</pre>
284286

287+
<h4>Using Converters</h4>
288+
289+
<p><code>$.ajax()</code> converters support mapping data types to other data types. If, however, you want to map a custom data type to a known type (e.g <code>json</code>), you must added a correspondance between the response Content-Type and the actual data type using the <code>contents</code> option:</p>
290+
291+
<pre>
292+
$.ajaxSetup({
293+
contents: {
294+
mycustomtype: /mycustomtype/
295+
},
296+
converters: {
297+
"mycustomtype json": function ( result ) {
298+
// do stuff
299+
return newresult;
300+
}
301+
}
302+
});
303+
</pre>
304+
305+
<p>This extra map is necessary because the response Content-Types and data types never have a strict one-to-one correspondance (hence the regular expression).</p>
306+
307+
<p>To convert from a supported type (e.g <code>text</code>, <code>json</code>) to a custom data type and back again, use another pass-through converter:</p>
308+
309+
<pre>
310+
$.ajaxSetup({
311+
contents: {
312+
mycustomtype: /mycustomtype/
313+
},
314+
converters: {
315+
"text mycustomtype": true,
316+
"mycustomtype json": function ( result ) {
317+
// do stuff
318+
return newresult;
319+
}
320+
}
321+
});
322+
</pre>
323+
324+
<p>The above now allows passing from <code>text</code> to <code>mycustomtype</code> and then <code>mycustomtype</code> to <code>json</code>.</p>
325+
285326
<h4>Extending Ajax</h4>
286327
<p><strong>As of jQuery 1.5</strong>, jQuery's Ajax implementation includes prefilters, converters, and transports that allow you to extend Ajax with a great deal of flexibility. For more information about these advanced features, see the <a href="http://api.jquery.com/extending-ajax/">Extending Ajax</a> page.</p>
287328
</longdesc>

0 commit comments

Comments
 (0)