Getting Started With jQuery - Advanced Ajax To The Client |
Written by Ian Elliot | ||||||||
Monday, 12 October 2015 | ||||||||
Page 4 of 4
An alternative multi-step approachIf you are implementing a completely new data type then creating a new text to newtype converter and registering a suitable header seems reasonable. However if what you have is a modification to an existing data type then perhaps a two or even multi-stage process is better. For example, jsond is just a development of JSON so why not accept JSON, allow jQuery to process it and then convert it to jsonp. For example, if the server sends a standard JSON header:
You can allow jQuery to perform the conversion to JSON and then create a converter from json to jsond:
Notice that now the dataType is jsond and we don't have to register a new header for the type. Also notice that the converter is now json to jsond rather than text to jsond. With these changes jQuery automatically calls its own text to json converter and then calls the json to jsond converter and notice that you don't have to explicitly convert the text data to JSON. If you don't want to rely on the header from the server you could write the dataType as
which forces the conversion sequence text->json->jsond no matter what the headers are. To summarise:you can create a custom data type by defining converter functions that convert text to your new data type. if you want jQuery to recognize a Content-Type header for your new data type you have to define a mapping from the new data type name to the specific header using a regular expression. Be careful that your regular expression isn't invalidated by an earlier expression that matches everything your's does. You can override existing data type converters in the same way. You can extend data type converters by chaining data types in the dataType option - the converters are called in the specified sequence. The dataFilterThere is one final function that you can place into jQuery's processing pipeline - the dataFilter. This is a function that is called right after the raw data has been received. The function is passed the raw data as its first parameter and the dataType specified as the second parameter. The function returns the raw data modified as required. For example, to include a dataFilter in the previous data converter program:
If you run this program with the PHP file given in the previous section as the server then you will see:
Which is the raw JSON string and the data type as set by the dataType option. The idea is that you use the dataFilter to "sanitize" or otherwise modify the raw data received before it is passed on to the converters. Where NextTopics that we have left to cover are how Script and JSONP work; global events, headers and how character encoding works. Summary
More InformationJust jQuery
|
JavaScript Canvas - Fetch API Working with lower-level data is very much part of graphics. This extract from Ian Elliot's book on JavaScript Graphics looks at how to use typed arrays to access graphic data. |
JavaScript Jems - The Inheritance Tax JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way. In particular, it doesn't do inheritance [ ... ] |
Other Articles |
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info