SlideShare ist ein Scribd-Unternehmen logo
1 von 3
Downloaden Sie, um offline zu lesen
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 +")." );
});
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
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/

Weitere ähnliche Inhalte

Andere mochten auch

Dangers of Cell phones सेलफोन – फ्रैंडली पिजन या ब्रेन बग
Dangers of Cell phones   सेलफोन – फ्रैंडली पिजन या ब्रेन बगDangers of Cell phones   सेलफोन – फ्रैंडली पिजन या ब्रेन बग
Dangers of Cell phones सेलफोन – फ्रैंडली पिजन या ब्रेन बग
Om Verma
 
Artifact 1 Powerpoint
Artifact 1 PowerpointArtifact 1 Powerpoint
Artifact 1 Powerpoint
jrweathe
 
Ensenyament Idiomes
Ensenyament IdiomesEnsenyament Idiomes
Ensenyament Idiomes
francescfs
 
Process Of Double Page Spread - Image
Process Of Double Page Spread - ImageProcess Of Double Page Spread - Image
Process Of Double Page Spread - Image
rosheen29
 

Andere mochten auch (11)

Dangers of Cell phones सेलफोन – फ्रैंडली पिजन या ब्रेन बग
Dangers of Cell phones   सेलफोन – फ्रैंडली पिजन या ब्रेन बगDangers of Cell phones   सेलफोन – फ्रैंडली पिजन या ब्रेन बग
Dangers of Cell phones सेलफोन – फ्रैंडली पिजन या ब्रेन बग
 
Marc0s
Marc0sMarc0s
Marc0s
 
final19
final19final19
final19
 
MAIORINO
MAIORINOMAIORINO
MAIORINO
 
Boletim (14)
Boletim (14)Boletim (14)
Boletim (14)
 
Artifact 1 Powerpoint
Artifact 1 PowerpointArtifact 1 Powerpoint
Artifact 1 Powerpoint
 
Ensenyament Idiomes
Ensenyament IdiomesEnsenyament Idiomes
Ensenyament Idiomes
 
Oral lang ece aps (july 28 2014) (final)
Oral lang ece aps (july 28 2014) (final)Oral lang ece aps (july 28 2014) (final)
Oral lang ece aps (july 28 2014) (final)
 
Animals
AnimalsAnimals
Animals
 
Process Of Double Page Spread - Image
Process Of Double Page Spread - ImageProcess Of Double Page Spread - Image
Process Of Double Page Spread - Image
 
Town of Princeton Show & Shine Princeton BC Canada July 05 2014
Town of Princeton Show & Shine Princeton BC Canada July 05 2014 Town of Princeton Show & Shine Princeton BC Canada July 05 2014
Town of Princeton Show & Shine Princeton BC Canada July 05 2014
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

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/