Added getajax function for jquery

This commit is contained in:
absurdo 2023-12-08 21:50:40 +01:00
parent d5c3b25447
commit 9c7b05577e

View file

@ -214,3 +214,154 @@
}
})( jQuery );
function sendGet(element, send_data, loading, success, error_data, pre_callback) {
if(element==undefined) {
throw 'Error: you need an element for use sendGet';
return false;
}
if(send_data==undefined) {
send_data={};
}
url=$(element).attr('url');
if(pre_callback!=undefined) {
options.pre_callback();
}
$.ajax({
url: url,
type: 'GET',
data: send_data,
success: function (data) {
$(options.loading).hide();
if(!data.error) {
if(options.hasOwnProperty("success")) {
options.success(data);
}
}
else {
if(options.hasOwnProperty("error_data")) {
options.error_data(data);
}
}
},
error: function (data) {
$(options.loading).hide();
alert('Error: '+data.status+' '+data.statusText);
},
dataType: 'json'
});
return false;
}
(function( $ ){
/* Add csrf token */
/* url: url to post is getted from object*/
/* element_id: element id, options:, loading: dom id, success: func, error_data: func, pre_callback, */
$.fn.sendGet = function (options)
{
/*if(!options.hasOwnProperty("url")) {
throw 'Error: you need an url for use sendPost';
return false;
}*/
if(!options.hasOwnProperty("loading")) {
options['loading']='#loading';
}
if(options.hasOwnProperty("pre_callback")) {
options.pre_callback(data);
}
//$(this).click( function (e) {
$(this).click( function(e) {
url=$(this).attr('data-url');
$(options.loading).show();
$.ajax({
url: url,
type: 'GET',
data: {},
success: function (data) {
$(options.loading).hide();
if(!data.error) {
if(options.hasOwnProperty("success")) {
options.success(data);
}
}
else {
if(options.hasOwnProperty("error_data")) {
options.error_data(data);
}
}
},
error: function (data) {
$(options.loading).hide();
alert('Error: '+data.status+' '+data.statusText);
},
dataType: 'json'
});
return false;
});
}
})( jQuery );