2012-10-10 100 views
3

大家好!jQuery AJAX自定義函數和自定義回調?

我有一個ajax()調用就像這樣:

$.ajax({ 
    type: "post", 
    url: "whatever.php", 
    data: { 
    theData: "moo moo" 
    }, 
    success: function(data) { 
    console.log(data); 
    } 
}); 

是否有可能來包裝這個自定義函數內,但保留回調?
喜歡的東西:

function customAjax(u, d, theCallbackStuff) { 
    $.ajax({ 
    type: "post", 
    url: u, 
    data: d, 
    success: function(data) { 
     //RUN theCallbackStuff 
    } 
    }); 
} 

theCallbackStuff將是這樣的:

var m = 1; 
var n = 2; 
alert(m + n + data); 
+0

你試過了嗎?我想這將是可能的 –

+0

因此,我可以在'theCallbackStuff'中保存一個完整的函數嗎? –

+0

我現在正在工作,所以我不能嘗試它 - 我在腦海中理論化;) –

回答

9

編輯:

得到了這個最近給予好評,我覺得有必要指出,我不會再這樣做了。 $.ajax返回promise,這樣您就可以直接使用承諾,以更加一致和可靠的方式完成我剛纔所做的工作。

function customRequest(u,d) { 
    var promise = $.ajax({ 
    type: 'post', 
    data: d, 
    url: u 
    }) 
    .done(function (responseData, status, xhr) { 
     // preconfigured logic for success 
    }) 
    .fail(function (xhr, status, err) { 
     //predetermined logic for unsuccessful request 
    }); 

    return promise; 
} 

然後使用看起來像:

// using `done` which will add the callback to the stack 
// to be run when the promise is resolved 
customRequest('whatever.php', {'somekey': 'somevalue'}).done(function (data) { 
    var n = 1, 
     m = 2; 

    alert(m + n + data); 
}); 

// using fail which will add the callback to the stack 
// to be run when the promise is rejected 
customRequest('whatever.php', {'somekey': 'somevalue'}).fail(function (xhr, status, err) { 
    console.log(status, err); 
}); 

// using then which will add callabcks to the 
// success AND failure stacks respectively when 
// the request is resolved/rejected 
customRequest('whatever.php', {'somekey': 'somevalue'}).then(
    function (data) { 
    var n = 1, 
     m = 2; 

    alert(m + n + data); 
    }, 
    function (xhr, status, err) { 
    console.log(status, err); 
    }); 

肯定我做這一切的時候。您可以執行實際成功callack內的回調或您可以指定回調的成功回調:

function customRequest(u,d,callback) { 
    $.ajax({ 
    type: "post", 
    url: u, 
    data:d, 
    success: function(data) { 
     console.log(data); // predefined logic if any 

     if(typeof callback == 'function') { 
      callback(data); 
     } 
    } 
    }); 
} 

用法看起來是這樣的:

customRequest('whatever.php', {'somekey': 'somevalue'}, function (data) { 
    var n = 1, 
     m = 2; 

    alert(m + n + data); 
}); 
+0

這正是我正在尋找的東西 - 雖然不應該是'==='? –

+0

你可以做'==='但在這種情況下它不會有問題,因爲'typeof'的結果將是一個字符串。 – prodigitalson

+0

最佳編輯我已經看到了一整天。非常感謝,...這是要走的路。 – MichaelClark

5
function customAjax(u, d, theCallbackStuff) { 
     $.ajax({ 
     type: "post", 
     url: u, 
     data: d, 
     success: theCallbackStuff 
     }); 
    } 

customAjax(url, data, function(data){ 
//do something 
}); 
+0

我會確保你使用'if(window.some_function_name_here)'或'如果(typeof funcname =='function')' –

1

關於這一點,你可以通過一個功能齊全的回調到這一點:

function customRequest(u,d,callback) { 
    $.ajax({ 
    type: "post", 
    url: u, 
    data:d, 
    success: function(data) { 
     console.log(data); // predefined logic if any 

     if(typeof callback == 'function') { 
      callback(data); 
     } 
    } 
    }); 
} 



// Then call it as follows: 

function initiator() { 

    customRequest('/url/to/post', 'param1=val', function() { alert('complete'); }) 

} 

簡單地把它當作一個匿名函數將工作太..只是爲了展示:)

着想