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 Powerpointjrweathe
 
Ensenyament Idiomes
Ensenyament IdiomesEnsenyament Idiomes
Ensenyament Idiomesfrancescfs
 
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)SVTaylor123
 
Process Of Double Page Spread - Image
Process Of Double Page Spread - ImageProcess Of Double Page Spread - Image
Process Of Double Page Spread - Imagerosheen29
 
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 P. Anna Paddon
 

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

WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceSamy Fodil
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Hiroshi SHIBATA
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...FIDO Alliance
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandIES VE
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentationyogeshlabana357357
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?Paolo Missier
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform EngineeringMarcus Vechiato
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftshyamraj55
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon
 
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxCyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxMasterG
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?Mark Billinghurst
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...panagenda
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxFIDO Alliance
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxFIDO Alliance
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGDSC PJATK
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 

Kürzlich hochgeladen (20)

WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptxCyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
Cyber Insurance - RalphGilot - Embry-Riddle Aeronautical University.pptx
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 

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/