Home > Web Front-end > JS Tutorial > Summary of common methods of jQuery calling ajax requests_jquery

Summary of common methods of jQuery calling ajax requests_jquery

WBOY
Release: 2016-05-16 16:08:02
Original
1067 people have browsed it

The examples in this article summarize the common methods of jQuery calling ajax requests. Share it with everyone for your reference. The details are as follows:

Sample code 1

$.ajax('/ROUTE', {
 type: 'GET'
 data: {param1: 'Hello', param2: 'World'},
 dataType: 'json',
 contentType: 'application/json',
 timeout: 3000,
 success: function(response) {
  // console.log(response.something);
 },
 error: function(request, errorType, errorMessage) {
  // console.log("[" + errorType + "] " + errorMessage);
 },
 beforeSend: function() {
  // do something like .addClass('is-fetching')
 },
 complete: function() {
  // do something like removeClass('is-fetching')
 }
});
Copy after login

Sample Code 2

$.get('/ROUTE', function(response) {
 // success (response: HTML)
});
 
$.getJSON('/ROUTE', function(response) {
 // success (response: JSON)
});
Copy after login

Sample code 3

$('form').on('submit', function(event) {
 event.preventDefault();
 var formData = $(this).serialize();
 $.ajax($(this).attr('action'), {
  type: $(this).attr('method'),
  data: formData,
  dataType: 'json',
  contentType: 'application/json',
  success: function(response) {},
  error: function(request, errorType, errorMessage) {},
  beforeSend: function() {},
  complete: function() {},
  timeout: 3000
 });
});
Copy after login

I hope this article will be helpful to everyone’s jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template