AJAX da jQuery- Buƙatun Gudanarwa da hulɗar Bayanai

AJAX(Asynchronous JavaScript da XML) fasaha ce da ke ba da damar sadarwa da musayar bayanai tsakanin mai lilo da uwar garken ba tare da buƙatar sake loda dukkan shafin yanar gizon ba. jQuery yana ba da hanyoyi masu dacewa da ayyuka don yin buƙatun AJAX. Ga wasu misalan amfani da AJAX tare da jQuery:

 

$.ajax() hanya

Hanyar $.ajax() hanya ce mai dacewa wacce ke ba ku damar yin buƙatun AJAX zuwa uwar garken. Yana ba da zaɓuɓɓuka daban-daban don keɓance buƙatun ku, kamar tantance URL, hanyar buƙatu(GET, POST, da sauransu), sarrafa nasara da kuskuren kiran da aka yi, da ƙari. Kuna iya amfani da wannan hanyar lokacin da kuke buƙatar iko mai kyau akan buƙatar AJAX.

$.ajax({  
  url: "data.php",  
  method: "GET",  
  success: function(response) {  
    // Handle successful response data  
  },  
  error: function(xhr, status, error) {  
    // Handle error occurred  
  }  
});  

 

$.get() hanya

Hanyar hanya $.get() ce ta gajeriyar hanya don yin buƙatar GET zuwa uwar garken. Yana sauƙaƙa aikin ta saita hanyar buƙatu ta atomatik zuwa GET da sarrafa nasarar dawowar kiran nasara. Kuna iya amfani da wannan hanyar lokacin da kawai kuke buƙatar dawo da bayanai daga cikin

$.get("data.php", function(response) {  
  // Handle successful response data  
});  

 

$.post() hanya

Hanyar $.post() tana kama da $.get(), amma tana aika buƙatar POST musamman ga uwar garken. Yana ba ku damar ƙaddamar da bayanai tare da buƙatar, wanda ke da amfani lokacin da kuke son aika bayanan tsari ko wasu sigogi zuwa uwar garken.

$.post("save.php", { name: "John", age: 30 }, function(response) {  
  // Handle successful response data  
});  

 

$.getJSON() hanya

Ana amfani da hanyar $.getJSON() don dawo da bayanan JSON daga uwar garken. Hanya ce ta gajeriyar hannu wacce ke saita hanyar buƙatu ta atomatik zuwa GET kuma tana tsammanin uwar garken zai dawo da martanin JSON. Yana sauƙaƙa tsarin maidowa da aiki tare da bayanan JSON.

$.getJSON("data.json", function(data) {  
  // Handle successful JSON response data  
});  

 

$.ajaxSetup() hanya

Hanyar $.ajaxSetup() tana ba ku damar saita saitunan tsoho don duk buƙatun AJAX na gaba. Misali, zaku iya saita tsoffin rubutun kai, saka nau'in bayanai, ko saita zaɓuɓɓukan tantancewa. Wannan hanyar tana da amfani lokacin da kake son saita zaɓuɓɓukan gama gari waɗanda suka shafi buƙatun AJAX da yawa.

$.ajaxSetup({  
  headers: { "Authorization": "Bearer token" }  
});  

 

$.ajaxPrefilter() hanya

Ana amfani da hanyar $.ajaxPrefilter() don gyara buƙatun AJAX kafin a aika su. Yana ba ku damar aiwatar da zaɓuɓɓukan buƙatun AJAX kuma canza su gwargwadon bukatunku. Wannan na iya zama da amfani don ƙara masu rubutun kai na al'ada, sarrafa bayanai, ko saƙon buƙatun.

$.ajaxPrefilter(function(options, originalOptions, xhr) {  
  // Preprocess before sending AJAX request  
});  

 

Waɗannan hanyoyin suna ba da hanyoyi daban-daban don yin aiki tare da buƙatun AJAX a cikin jQuery. Dangane da takamaiman bukatunku, zaku iya zaɓar hanyar da ta fi dacewa da bukatunku. jQuery yana sauƙaƙa tsarin yin buƙatun AJAX da sarrafa martani, yana ba ku damar ƙirƙirar aikace-aikacen yanar gizo masu ƙarfi da mu'amala.