本文实例汇总了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程序设计有所帮助。
本文详细介绍了jQuery中$.ajax、$.get和$.getJSON等方法调用Ajax请求的实例,涵盖了GET、JSON数据类型、请求头设置、错误处理等功能,帮助开发者更好地理解和运用jQuery进行异步数据交互。
254

被折叠的 条评论
为什么被折叠?



