How can i use this function always but only extend the success: function..
to my seperate callback?
global.js
// one time written and will be uesd 100 times without re-write the same thing.
function useEveryWhere(url, par) {
$.ajax({
type: "POST",
dataType: "json",
url: url, //PHP call
data: par,
async: true,
success : function(msg) {}
});
}
anotherpage1.js
// Here i need the success: function with different task
ueEveryWhere(url, parameters);
anotherpage2.js
// Here i need the success: function with different task too not same as anotherpage1.js
ueEveryWhere(url, parameters);
How do i have that global.js success method in any other scripts too, where i have random task not all are same.
You can use the ajaxSucess function to specify a global sucess method, and change useEveryWhere
to accept a success function:
function useEveryWhere(url, par, sucess) {
$.ajax({
type: "POST",
dataType: "json",
url: url, //PHP call
data: par,
async: true,
success : sucess
});
}
anotherpage1.js
useEveryWhere(url, parameters, function() { ... });
anotherpage2.js
useEveryWhere(url, parameters, function() { ... });
You can use Global AJAX Event Handlers with jQuery: http://api.jquery.com/category/ajax/global-ajax-event-handlers/
Just add: $.ajaxSuccess(function () { /*code here*/ });
and every AJAX response that is a success will run the code.
Use jQuery $.Deferred
objects:
$.when(useEveryWhere(url, parameters)).done(function() { /* callback 1 */ });
$.when(useEveryWhere(url, parameters)).done(function() { /* callback 2 */ });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.