欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery调用ajax请求的常见方法汇总

程序员文章站 2022-06-21 19:30:34
本文实例汇总了jquery调用ajax请求的常见方法。分享给大家供大家参考。具体如下: 示例代码1 $.ajax('/route', { type:...

本文实例汇总了jquery调用ajax请求的常见方法。分享给大家供大家参考。具体如下:

示例代码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')
 }
});

示例代码2

$.get('/route', function(response) {
 // success (response: html)
});
 
$.getjson('/route', function(response) {
 // success (response: json)
});

示例代码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
 });
});

希望本文所述对大家的jquery程序设计有所帮助。