当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQuery调用ajax请求的常见方法汇总

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

2019年05月17日  | 移动技术网IT编程  | 我要评论

本文实例汇总了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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网