Getting Started With jQuery - Advanced Ajax jqXHR and Events |
Written by Ian Elliot | ||||||
Monday, 18 January 2016 | ||||||
Page 2 of 2
Custom HeadersTypical uses of beforeSend are to set any request headers you might need. For example:
If you examine the headers at the server - try:
you will find that there is a new header:
You can also specify a header or set of headers using the options. It can be done in the headers option. for example
sets the same custom header. The only differnce between the two methods is perhaps visibility to the programmer working at a higher level. If you use the beforeSend function then the setting of the header is slightly less visible than the options method. It also overrides any header set in the options. There are lot of reasons for setting custom headers. One major reason is to perform cross domain requests using CORS. As this involves the server to a great extent it is beyond the scope of this chapter - email me if you would like to see an article that deals with using jQuery to implement CORS. Another common use of custom headers is to send a user name and password as part of HTTP authentication. To make things easier in this case jQuery provides the username and password options which can be set to the appropriate user name and password and automatically generated the necessary authentication headers. Setting HTTP code handlers.There is a facility in jQuery to define handlers that are automatically called according to the HTTP code returned by the server. You can, of course use the jsXHR status property and an if statement within the done or fail methods to explicitly call status code handlers but if you register a status code handler using the statusCode function it will be called automatically. For example,
This passed to the statusCode function a map of key value pairs. In this case the value is the HTTP code and the value is the function to be called when that HTTP code is returned. You can achieve exactly the same result using the statusCode option:
Again which one is best to use depends on at what point in the request you want to attach the handlers. The Local and Global Event HandlersIn most cases you only need to make use of the Promise that the ajax method returns to handle the result of an Ajax request. However before the use of the Promise object there were and still are the old event handlers. These are deprecated and you shouldn't use them if at all possible but you might need to know about them:
The call sequence of all of the event handlers is:
Notice that the Promise methods can have multiple handlers assigned to them and they will all be executed and in the order that they were defined. The old event handlers can only have a single function to execute. As well as these "local" events there are also global event handlers that can be set to apply to all Ajax requests. However it is important to note that global event handlers are never invoked during a JSONP or a cross domain script request as these do not use the XmlHttpRequest object for transport. All of the global handlers accept an event handler with the same form:
They all also have to be attached to the document object. The relationship between the global and local events is well described in the documentation:
Notice that the ajaxStart/Stop events can be used to keep track of overall Ajax activity. Apart from this the global events are simply alternatives to the local events. For example you could use
in place of the beforeSend function. You can also turn off all global events by setting the global option to false. Finally there is the context option. By default in all event handlers and callbacks this is set to object that is the result of merging the options set by ajaxSettings and the options specified in the call. So for example you could use:
in place of options.url. Alternatively you can set this to any object that might be useful using the context option. For example:
This sets this to a jQuery object wrapping the portion of the DOM corresponding to the body tag. After this you can write things like:
Notice that html() is a jQuery method and this is a jQuery result object. This sort of technique is only likely to be useful in situations where you need to change the object that the event handlers/callbacks operate on. In most cases it is easier to do the processing on the object explicitly e.g.
Where NextThe topic we still have to consider is 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