Getting Started With jQuery - Advanced Ajax Transports |
Written by Ian Elliot | ||||
Monday, 09 November 2015 | ||||
Page 2 of 3
ServersideAt the server the only task is to wrap some JSON in a function wrapper and return the result as if it was a script;
If you try this out you will be disappointed to discover that nothing works. The Ajax then function is never called because jQuery uses a different method to detect the completion of a JSONP request. When the script is run it attempts to call myFunction with the JSON data but of course there is no myFunction. We can remedy this by adding a myFunction to our client side program:
Now when you run the program you will see the JSON string in the console.
Again the Ajax request never seems to complete and again this is because we haven't supplied the sort of wrapper function that jQuery expects. To know that the JSONP request has completed jQuery has to "know" the name of the wrapper function. if you set the jsonpCallback option to the name of the wrapper function jsonpCallbackSets the name of the callback function that the server wraps the JSON data in and is implemented in the client. For example is you add:
then jQuery will hook into myFunction and fire the Promise object's done method when it runs. Now if you run the program you will see the JSON string twice - once from the myFunction wrapper and once from the Ajax done method. This works no matter which of the two transports is used. We can even avoid having to define an explicit wrapper function within the client. Instead you can get jQuery to pass the name of a suitable wrapper function to the server by simply not specifying a callback. By default jQuery will add a query string callback=long random number. It also creates a callback function with the same random name that simply calls the then method as if an Ajax call had just completed. Of course to make this work the server has to add a wrapper function with the name specified by the client - but this is easy:
All that happens different is that now the server retrieves the query string and uses it as the name of the function to wrap the JSON. Now if you run the client:
then you will see the JSON data in the console just as if this was a standard Ajax call. If you want to take more control of the transaction then you might need to specify the jsonp option to change the name of the query string passed to the server. jsonpSet the name of the key passed to the server value of the callback name within the URL. So for example if the server wants a query string called jsonRequest you can make the client send it using
Now the client will send jsonRequest=some random number to the sever and of course the server has to get the jsonRequest parameter
You can also stop jQuery sending any callback parameter by setting jsonp to false. In this case you do need to set jsonpCallback to the name of the callback wrapper function or you have to remember to provide your own function in the client code. To summarise:
A couple of final points. If you make a cross domain request then script transport is used and you will not get the same range of error handling. Cross domain JSONP is an inherent security risk because you are relying on the server not to return any dangerous code.
Custom transportThe idea that jQuery can use different transports to implement an Ajax request is a powerful one. You can add your own transports to jQuery very easily - however implementing the new transport on the server is usually more difficult. To add a new transport to jQuery all you have to do is use the ajaxTransport method. ajaxTransport(dataType,handler)Add a new transport method for the named data type. The only complicated part about ajaxTransport is that the function that you specify as the second parameter doesn't implement the transport it simply returns an object that does i.e. it is a transport object factory. The definition of the transport object factory is:
where options are the request options including any defaults, originalOptions are the options explicitly set by the user and jqXHR is the jqXHR object constructed for the request. The jqXHR object has a lot of additional information about the request including the url etc. The transport object factory has to return a transport object which has just two methods send and abort:
This just has to implement the transport and call completeCallback when the transport is completed. It can use the headers passed into the function as necessary.
This simply has to abort the transport - which is often easier said than done! The final function to define in this set is completeCallback:
Where status and statusText give the reponse code and text, responses is a set of dataType key value pairs i.e. it is the payload of the transport and headers is again a set of optional response headers. Let's implement the simplest example of a new transport which returns a fixed string immediately. The transport object factory is:
Notice that all this does is to construct an object with two properties send and abort. The send function calls the callback function to signal that it is complete at once. It sends back the success status code and text and the data "this is the data". Notice that the data type has to be the type that the transport is designed to handle otherwise a converter will be called. To register the new transport all we have to do is:
The entire program is:
In this case the url and the method are fairly irrelevant. Notice that the datatype has to be mydatatype. If the transport returns alternative data types then you might need a converter. If you run the program you should see "this is the data" in the console. Now that you have seen a simple example you are probably wondering what sorts of things you could implement using a custom transport. The first thing to notice that that you could use the simple example as a testing harness for Ajax programs so it isn't completely useless. |
||||
Last Updated ( Thursday, 05 May 2022 ) |