Added getajax function for jquery
This commit is contained in:
parent
d5c3b25447
commit
9c7b05577e
1 changed files with 151 additions and 0 deletions
151
posting2.js
151
posting2.js
|
|
@ -214,3 +214,154 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
})( jQuery );
|
})( 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 );
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue