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