這篇文章主要介紹了jQuery調用ajax請求的常見方法,實例匯總了三種常見的jQuery調用Ajax的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例匯總了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程序設計有所幫助。
新聞熱點
疑難解答
圖片精選