JQuery.ajax how to use content property

Can someone give me an example of how to use the contents property?

 $.ajax( { contents : {...} } 

JQuery's own documentation only states the following:

<strong> contents

An object of string/regular-expression pairs that determine how jQuery will parse the response, given its content type

+6
source share
2 answers

From http://api.jquery.com/jQuery.ajax/ :

$. Ajax () converters support the mapping of data types to other data types. If, however, you want to map a custom data type to a known type (e.g. json), you must add a mapping between the Content-Type and the actual data type using the content option:

 $.ajaxSetup({ contents: { mycustomtype: /mycustomtype/ }, converters: { "mycustomtype json": function ( result ) { // do stuff return newresult; } } }); 

So what does this mean, your server response data type may be mycustomtype . And when the AJAX call receives the data, it will see that its data type is mycustomtype and matches the regular expression that we set (in the content): /mycustomtype/ and outputs the data to the converter we specified, for example. mycustomtype json() will try to convert the data to json.

More about converters :

converters["mycustomtype json"] means that the function specified here will be executed if you want to convert the format of mycustomtype to json , so the contents of this function will parse which will return json . You can specify other types of conversions, for example.

 converters: { "mycustomtype json": converterFromCustomTypeToJson, // mycustomtype -> json "mycustomtype jsonp": converterFromCustomTypeToJsonP, // mycustomtype -> jsonp "mycustomtype text": converterFromCustomTypeToText, // mycustomtype -> text "text mycustomtype": converterFromTextToCustomType // text -> mycustomtype } 

And you should write your own converter functions:

 var converterFromCustomTypeToJson = function(result) { var jsonResult = ... /* do conversion to JSON */ return jsonResult; }, converterFromCustomTypeToJsonP = function(result) { var jsonPResult = ... /* do conversion to JSONP */ return jsonPResult; }, converterFromCustomTypeToText = function(result) { var textResult = ... /* do conversion to text */ return textResult; }, converterFromTextToCustomType = function(result) { var customResult = ... /* do conversion to mycustomtype */ return customResult; }; 

You would also like to avoid clutter with the default converters:

 { "* text": window.String, "text html": true, "text json": jQuery.parseJSON, "text xml": jQuery.parseXML } 
+5
source

contents used to define new data types. There are 4 default data types in jQuery: xml, json, script, and html. If dqataType is not specified for ajax call, jQuery will try to infer it based on the MIME type of the response. When defining a new data type using contents you must specify a regular expression expression that will be used to output the user data type from the MIME response type.

For ex, to define a CSV dataType that would otherwise be parsed as text:

 $.ajaxSetup({ // for: text/csv or application/csv contents: { csv: /csv/ } }); 

contents used in conjunction with converters:

To convert csv to json, define a custom converter:

 $.ajaxSetup({ contents: { csv: /csv/ }, converters: { "csv json": function (result) { // parse csv here return jsonresult; } } }); 
+2
source

All Articles