Przegląd metod jQuery Ajax

Najpierw rozpowszechnijmy słowa jQuery i ajax i znamy dosłowne znaczenie każdego z nich. JQuery to biblioteka javascript, która jest bardzo lekka i mówi tylko, że pisz mniej, a więcej. Ale zanim przejdziesz do jQuery, musisz znać podstawy html, CSS i javascript. Głównym zadaniem jQuery jest sprawienie, aby zadanie programisty było łatwe do renderowania Javascript w przeglądarce z dużą łatwością i w bardzo krótkim czasie. Jednym słowem możemy powiedzieć, że jQuery zamienia ciężką linię kodu javascript w pojedynczy wiersz kodu przy użyciu metod jQuery. jQuery upraszcza również skomplikowany kod javascript, dom-manipulację i wywoływanie ajax.

W jQuery dostępne są następujące funkcje wymienione poniżej:

  • Metody zdarzeń HTML
  • Manipulacja CSS
  • Manipulacja DOM / HTML
  • AJAX
  • Efekty i animacje
  • Narzędzia

Myślę, że teraz masz podstawową wiedzę o jQuery, więc przejdźmy do znaczenia AJAX. Ajax oznacza asynchroniczny javascript i XML (rozbudowany język znaczników). Ajax to najprostszy i najłatwiejszy sposób tworzenia dynamicznych stron internetowych. Ajax mówi nam, że wymieniamy dane bez odświeżania strony internetowej. Oznacza to, że możemy wymieniać niewielką ilość danych z serwerem bez odświeżania strony lub innymi słowy, możemy powiedzieć, że ajax aktualizuje stronę bez ponownego ładowania strony. Na klasycznych stronach internetowych muszą ponownie załadować całą stronę, jeśli chcą wymieniać dane z serwerem lub chcą zaktualizować część strony. Omówmy w skrócie, jak to działa.

Zasadniczo co się dzieje, ilekroć wystąpi zdarzenie, przeglądarka tworzy obiekt XMLHttpRequest i wysyła HttpRequest do serwera. Następnie serwer przetwarza to żądanie HTTP i tworzy odpowiedź i odsyła dane z powrotem do przeglądarki. Teraz przeglądarka przetwarza zwrócone dane za pomocą javascript i aktualizuje zawartość strony.

Po zrozumieniu obu tych terminów nadszedł czas na omówienie tematu. Czym jest jQuery ajax? jQuery zapewnia wiele metod, które pomagają w aktualizacji stron WWW po stronie serwera. Składnia dla jQuery ajax to jQuery.ajax (url (, ustawienia)) , gdzie URL jest ciągiem, z którego wysyłane jest żądanie. Ustawienie to typ zwykłego obiektu. Metody ajaxowe jQuery zapewniają po prostu pewną cenną, zasobną funkcjonalność dla ajax w jQuery. Aneks ma adres URL i taką opcję:

$ .ajax (url);

$ .ajax (url, (opcje));

Tutaj URL jest rodzajem ciągu, przez który możemy wysyłać i odbierać dane, a opcje to konfiguracja żądania ajax. Istnieje wiele opcji dostępnych w jQuery ajax.

Omówmy niektóre z nich.

  1. Akceptuje: informuje serwer, jaki rodzaj odpowiedzi w zamian zaakceptuje.
  2. Asynchronizacja: musimy ustawić wartość false, jeśli chcemy wysyłać dane synchronicznie, ponieważ domyślnie dane są wysyłane lub odbierane asynchronicznie.
  3. BeforeSend: oznacza to, że przed wysłaniem żądania ajax należy wykonać funkcję zwrotną.
  4. Pamięć podręczna: domyślnie jest to prawda. Pamięć podręczna przeglądarki jest wyświetlana przez wartość logiczną.
  5. Complete : po zakończeniu żądania należy wykonać funkcję zwrotną.
  6. contentType : Domyślny contentType to „application / x-www-form-urlencoded; charset = UTF-8 ”.
  7. crossDomain : wartość logiczna oznacza, czy przetworzone żądanie jest między domenami, czy nie.
  8. Dane: są to dane w postaci tablicy, łańcucha lub obiektu, które należy wysłać na serwer.
  9. dataType: jest to typ danych, które serwer oczekuje na powrót.
  10. Błąd: oznacza, że ​​funkcja wywołania zwrotnego musi zostać wykonana, gdy żądanie nie powiedzie się.
  11. Globalny: domyślnie jest to prawda. Daje boolowską odpowiedź, czy aktywować żądanie globalne, czy nie.
  12. Nagłówki: obiekt musi zostać wysłany wraz z żądaniem.
  13. ifModified: jeśli żądanie zmieniło się od czasu ostatniej zaktualizowanej prośby, tylko odpowiedź jest poprawna.
  14. isLocal: pozwala rozpoznać obecne lub obecne środowisko programistyczne
  15. Jsonp: w żądaniu jsonp zastępuje funkcję zwrotną.
  16. jsonpCallback: jest to typ łańcucha, który zawiera jsonprequest w nazwie funkcji zwrotnej.
  17. mimeType: zawiera typ mime, który zastępuje xmlhttprequest.
  18. hasło: jest używane w żądaniu uwierzytelnienia HTTP.
  19. processData: domyślnie jest to prawda, daje tylko odpowiedź logiczną, czy dane przypisane do opcji danych powinny zostać przekonwertowane na ciąg zapytania, czy nie.
  20. statusCode: jest to numeryczny kod żądania HTTP, na który odpowiada się zgodnie z odpowiednią odpowiedzią na kod.
  21. Sukces: jeśli próba httprequest zakończyła się powodzeniem, należy wykonać tylko funkcję zwrotną.
  22. Limit czasu: jest to wartość limitu czasu odpowiedzi w milisekundach.
  23. Typ: jest to rodzaj httprequestu, takiego jak GET, PUT, POST. Domyślnie jest to GET.
  24. url: to ciąg znaków zawierający adres URL, do którego należy wysłać dane.
  25. Nazwa użytkownika : jest również używany w żądaniu uwierzytelnienia XMLHTTP.
  26. Xhr: funkcja wywołania zwrotnego służy do tworzenia obiektu xmlhttprequest.
  27. xhrFields: jest to para ustawiana na wartość fieldName-fieldValue.

Metody jQuery Ajax

Teraz omówimy niektóre metody jQuery ajax z jego składnią i przykładami. Poniżej wymieniono metody jQuery.

1. jQuery.get (adres URL, (dane), (oddzwanianie), (typ))

Ta metoda pomaga nam w ładowaniu danych z serwera za pomocą żądania GET HTTP. Ta metoda zwraca również obiekt XMLHttpRequest.

  • url - to ciąg znaków, który zawiera adres URL, przez który przesyłane są dane.
  • Dane - jest to opcjonalny parametr zawierający parę kluczy i wartości, który zostanie wysłany na serwer.
  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.
  • Typ - jest to również parametr opcjonalny, który zwraca typ danych po funkcji wywołania zwrotnego, taki jak html, xml, json, text, jsonp.

Przykład:

$(document).ready(function() (
$("#driver2").click(function(event)(
$.get(
"result.php",
( name: "Zara2" ),
function(data) (
$('#stage2').html(data);
)
);
));
));

2. jQuery.getJSON (adres URL, (dane), (oddzwanianie))

Ta metoda ładuje dane json z serwera za pośrednictwem żądania GET HTTP.

  • url - to ciąg zawierający adres URL, przez który dane są wysyłane.
  • Dane - jest to opcjonalny parametr zawierający parę kluczy i wartości, który zostanie wysłany na serwer.
  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.

Przykład:

$(document).ready(function() (
$("#driver2").click(function(event)(
$.getJSON('result.json', function(jd) (
$('#stage2').html('

Nazwa: „+ jd.name +”

”);
$ ('# stage2'). append ('

Wiek: „+ jd.age +”

”);
$ ('# stage2'). append ('

Płeć: „+ jd.sex +”

”);
));
));
));

3. jQuery.getScript (url, (callback))

Ta metoda ładuje i wykonuje plik javascript za pośrednictwem żądania HTTP GET.

  • url - to ciąg zawierający adres URL, przez który dane są wysyłane.
  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.

Przykład:

$(document).ready(function() (
$("#driver2").click(function(event)(
$.getScript('result.js', function(jd) (
// Call custom function defined in script
CheckJS();
));
));
));

4. jQuery.post (adres URL, (dane), (oddzwanianie), (typ))

Ta metoda ładuje stronę internetową za pomocą żądania HTTP POST.

  • url - to ciąg znaków, który zawiera adres URL, przez który przesyłane są dane.
  • Dane - jest to opcjonalny parametr zawierający parę kluczy i wartości, który zostanie wysłany na serwer.
  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.
  • Typ - jest to również parametr opcjonalny, który zwraca typ danych po funkcji wywołania zwrotnego, taki jak html, xml, json, text, jsonp.

Przykład:

$(document).ready(function() (
$("#driver2").click(function(event)(
$.post(
"result.php",
( name: "Zara" ),
function(data) (
$('#stage2').html(data);
)
);
));
));

5. ładowanie (adres URL, dane, oddzwanianie)

Ta metoda ładuje dane lub obiekt z serwera i zastępuje zwrócony HTML po żądaniu sukcesu do dopasowanego elementu.

  • url - to ciąg zawierający adres URL, przez który dane są wysyłane.
  • Dane - jest to opcjonalny parametr zawierający parę kluczy i wartości, który zostanie wysłany na serwer.
  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.

Przykład:

$(document).ready(function() (
$("#drive2r").click(function(event)(
$('#stage2').load('result.html');
));
));

6. serialize ()

Ta metoda pomaga w serializacji zestawu elementów wejściowych do zestawu tablicy danych. W tej metodzie jQuery ajax nie ma parametru. Na przykład-

$(document).ready(function() (
$("#driver2").click(function(event)(
$.post(
"/jquery/serialize.php",
$("#testform1").serialize(),
function(data) (
$('#stage2').html(data);
)
);
var str = $("#testform1").serialize();
$("#stage3").text(str);
));
));

7. serializeArray ()

Ta metoda działa tak samo jak metoda serializacji, z tą różnicą, że zwraca strukturę danych JSON. Ta metoda również nie ma żadnych parametrów. Na przykład-

$(document).ready(function() (
$("#driver2").click(function(event)(
$.post(
"/jquery/serialize.php",
$("#testform1").serializeArray(),
function(data) (
$('#stage2').html(data);
)
);
var fields = $("#testform1").serializeArray();
$("#stage3").empty();
jQuery.each(fields, function(i, field)(
$("#stage3").append(field.value + " ");
));
));
));

8. ajaxComplete (oddzwanianie)

Ta metoda łączy funkcję, gdy funkcja wywołania zwrotnego zostanie wykonana pomyślnie.

  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.

Przykład:

$(document).ready(function() (
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxComplete(function(event, request, settings)(
$("#stage3").html(" Request Complete. ");
));
));

$(document).ready(function() (
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxComplete(function(event, request, settings)(
$("#stage3").html(" Request Complete. ");
));
));

$(document).ready(function() (
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxComplete(function(event, request, settings)(
$("#stage3").html(" Request Complete. ");
));
));

9. ajaxStart (callback)

Ta metoda dołącza funkcję do wykonania przed uruchomieniem żądania ajax.

  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.

Przykład:

$(document).ready(function() (
/* Global variable */
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
/* Gets called when the request starts */
$(document).ajaxStart(function()(
count++;
$("#stage3").html(" Starts, Count :" + count + " ");
));
/* Gets called when request complete */
$(document).ajaxComplete(function(event, request, set)(
count++;
$("#stage4").html(" Completes, Count:" + count + " ");
));
));

$(document).ready(function() (
/* Global variable */
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
/* Gets called when the request starts */
$(document).ajaxStart(function()(
count++;
$("#stage3").html(" Starts, Count :" + count + " ");
));
/* Gets called when request complete */
$(document).ajaxComplete(function(event, request, set)(
count++;
$("#stage4").html(" Completes, Count:" + count + " ");
));
));

$(document).ready(function() (
/* Global variable */
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
/* Gets called when the request starts */
$(document).ajaxStart(function()(
count++;
$("#stage3").html(" Starts, Count :" + count + " ");
));
/* Gets called when request complete */
$(document).ajaxComplete(function(event, request, set)(
count++;
$("#stage4").html(" Completes, Count:" + count + " ");
));
));

$(document).ready(function() (
/* Global variable */
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
/* Gets called when the request starts */
$(document).ajaxStart(function()(
count++;
$("#stage3").html(" Starts, Count :" + count + " ");
));
/* Gets called when request complete */
$(document).ajaxComplete(function(event, request, set)(
count++;
$("#stage4").html(" Completes, Count:" + count + " ");
));
));

$(document).ready(function() (
/* Global variable */
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
/* Gets called when the request starts */
$(document).ajaxStart(function()(
count++;
$("#stage3").html(" Starts, Count :" + count + " ");
));
/* Gets called when request complete */
$(document).ajaxComplete(function(event, request, set)(
count++;
$("#stage4").html(" Completes, Count:" + count + " ");
));
));

10. ajaxError (callback)

Ta metoda dołącza funkcję wykonywaną za każdym razem, gdy żądanie ajax nie powiedzie się. Jest to także rodzaj zdarzenia ajax.

  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.

Przykład:

$(document).ready(function() (
$("#driver2").click(function(event)(
/* Assume result.text does not exist. */
$('#stage12').load('/jquery/result.text');
));
$(document).ajaxError(function(event, request, settings )(
$("#stage22").html(" Error in front page. ");
));
));

$(document).ready(function() (
$("#driver2").click(function(event)(
/* Assume result.text does not exist. */
$('#stage12').load('/jquery/result.text');
));
$(document).ajaxError(function(event, request, settings )(
$("#stage22").html(" Error in front page. ");
));
));

$(document).ready(function() (
$("#driver2").click(function(event)(
/* Assume result.text does not exist. */
$('#stage12').load('/jquery/result.text');
));
$(document).ajaxError(function(event, request, settings )(
$("#stage22").html(" Error in front page. ");
));
));

11. ajaxSend (callback)

Ta metoda wywołuje funkcję zaraz po wysłaniu żądania ajax. To jest także wydarzenie ajax.

  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.

Przykład:

$(document).ready(function() (
var count = 0;
$("#drive2r").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage23").html(" Sends, Count :" + count + " ");
$("#stage23").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage33").html(" Completes, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#drive2r").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage23").html(" Sends, Count :" + count + " ");
$("#stage23").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage33").html(" Completes, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#drive2r").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage23").html(" Sends, Count :" + count + " ");
$("#stage23").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage33").html(" Completes, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#drive2r").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage23").html(" Sends, Count :" + count + " ");
$("#stage23").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage33").html(" Completes, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#drive2r").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage23").html(" Sends, Count :" + count + " ");
$("#stage23").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage33").html(" Completes, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#drive2r").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage23").html(" Sends, Count :" + count + " ");
$("#stage23").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage33").html(" Completes, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#drive2r").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage23").html(" Sends, Count :" + count + " ");
$("#stage23").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage33").html(" Completes, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#drive2r").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage23").html(" Sends, Count :" + count + " ");
$("#stage23").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage33").html(" Completes, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#drive2r").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage23").html(" Sends, Count :" + count + " ");
$("#stage23").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage33").html(" Completes, Count :" + count + " ");
));
));

12. ajaxStop (oddzwanianie)

Ta metoda dołącza funkcję, która jest wykonywana zaraz po zakończeniu żądania ajax.

  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.

Przykład:

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 0;
$("#driver2").click(function(event)(
$('#stage1').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage3").html(" Completes, Count :" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage44").html(" Stops, Count :" + count + " ");
));
));

13. ajaxSuccess (callback)

Ta metoda wywołuje funkcję zaraz po pomyślnym zakończeniu żądania ajax.

  • Oddzwanianie - jest to również parametr opcjonalny, co oznacza funkcję, która ma zostać wykonana po pomyślnym wysłaniu żądania ajax.

Przykład:

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

$(document).ready(function() (
var count = 2;
$("#driver2").click(function(event)(
$('#stage2').load('result.html');
));
$(document).ajaxStart(function()(
count++;
$("#stage12").html(" Starts, Count :" + count + " ");
));
$(document).ajaxSend(function(evt, req, set)(
count++;
$("#stage22").html(" Sends, Count :" + count + " ");
$("#stage22").append(" URL :" + set.url + " ");
));
$(document).ajaxComplete(function(event, request, settings)(
count++;
$("#stage32").html(" Completes, Count:" + count + " ");
));
$(document).ajaxStop(function(event, request, settings)(
count++;
$("#stage42").html(" Stops, Count :" + count + " ");
));
$(document).ajaxSuccess(function(event, request, settings)(
count++;
$("#stage52").html(" Success, Count :" + count + " ");
));
));

Teraz przyjmijmy znaczenie metod jQuery ajax, które są wymienione poniżej:

  • Jest kompatybilny z wieloma przeglądarkami i obsługuje prawie wszystkie przeglądarki.
  • Jest to najprostsza metoda do zastosowania podczas aktualizacji zawartości strony bez ponownego ładowania strony.
  • Ma moc wysyłania żądań POST i GET.
  • Ma możliwość ładowania HTML, JSON, XML lub skryptów.

Polecane artykuły

Jest to przewodnik po metodach jQuery Ajax. Tutaj omawiamy niektóre metody jQuery Ajax ze składnią i przykładami. Możesz także zapoznać się z poniższym kursem analizy danych, aby dowiedzieć się więcej

  1. Metody jQuery
  2. jQuery Alternatywy
  3. Jak zainstalować Jquery?
  4. Ściągawka JQuery
  5. jQuery querySelector
  6. Top 5 atrybutów zdarzeń HTML z przykładami
  7. 8 najlepszych selektorów jQuery z implementacją kodu
  8. Przewodnik po przykładach paska postępu JQuery