Comparing Sidecar-less Service Mesh from Cilium and Istio
Ajax jquery
1. How to Use jQuery’s $.ajax() Function
In our previous blog(What is AJAX and How it works ?), we discussed about AJAX and how to
implement the same using jQuery.
We did try to learn the same with the help of an basic example. In this tutorial, we are going to take
it from there and will try to understand about the various available parameter in ajax function.
Though we can’t cover all as it is a long list which you can read here but we shall cover the
important parameters.
But before that, let’s take another example.
var request = $.ajax({
type: "POST",
url: "submit.php",
data: { username: $('#username').val(), email: $('#email').val() }
});
request.done(function(response, textStatus, jqXHR)
{
alert( response );
});
request.fail(function(jqXHR, textStatus, errorThrown)
{
alert( "The following error occured (" + textStatus + "," + errorThrown +")." );
2. });
In this example, Success and fail cases have been implemented. Upon receiving a successful
response from server, request.done callback will be executed else request.fail will be executed.
There is another callback function named as “always()”, which will be executed irrespective of
successful and failed response.
jqXHR.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) { });
.always() method replaces the deprecated .complete() method.
Till now, we have covered the success and fail method along with always method, using which you
can successfully implement your AJAX requests.
Now as we discussed in out previous blog () about the parameters of ajax function so let’s learn
more about them.
accepts: The content type sent in the request header that tells the server what kind of response it
will accept in return.
async: By default, all requests are asynchronous. If you need synchronous requests, set this option
to false.
beforeSend: A pre-request callback function that can be used to modify the jqXHR object before it
is sent.
cache: If set to false, it will force requested pages not to be cached by the browser.
complete: A function to be called when the request finishes (after success and error callbacks are
executed).
contents: An object of string/regular-expression pairs that determine how jQuery will parse the
response.
contentType: Set the content type of the data sent to the server.
context: An object to use as the context (this) of all Ajax-related callbacks.
converters: An object containing dataType-to-dataType converters.
crossDomain: If you want to make a crossDomain request then set this property to true
data: Data to be sent to the server. It is converted to a query string, if not already a string.
dataFilter: A function to be used to handle the raw response data of XMLHttpRequest. This is a
pre-filtering function to sanitize the response.
dataType: The type of data expected back from the server
error: A function to be called if the request fails
global: Whether to trigger global Ajax event handlers for this request
3. headers: An object of additional headers to send to the server
ifModified: Set this option to true if you want to force the request to be successful only if the
response has changed since the last request
isLocal: Set this option to true if you want to force jQuery to recognize the current environment as
“local”
Please visit jquery official documentation to learn more about the rest of parameters.
References:
[1].http://www.calipus.com/blog/how-to-use-jquerys-ajax-
function/
[2].http://api.jquery.com/jquery.ajax/