From 9c7b05577e4144aa04851126cc1dfec89088a429 Mon Sep 17 00:00:00 2001 From: absurdo Date: Fri, 8 Dec 2023 21:50:40 +0100 Subject: [PATCH] Added getajax function for jquery --- posting2.js | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/posting2.js b/posting2.js index 2403a0c..226bf65 100644 --- a/posting2.js +++ b/posting2.js @@ -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 );