Fixes in posting2
This commit is contained in:
parent
d07a2622cc
commit
db37319c90
1 changed files with 57 additions and 66 deletions
123
posting2.js
123
posting2.js
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
/* Add csrf token */
|
||||
|
||||
/* options: url: url to post, loading: dom id, success: func, pre_callback, separated_data:boolean */
|
||||
/* options: url: url to post, loading: dom id, success: func, pre_callback, separated_data:boolean, upload: {progressbar: '#progressbar', 'total_loader': '#total_loader', 'status': '#status'} */
|
||||
|
||||
$.fn.sendPost = function (options)
|
||||
{
|
||||
|
|
@ -25,20 +25,37 @@
|
|||
|
||||
var xhr=false;
|
||||
|
||||
if(!options.hasOwnProperty("upload")) {
|
||||
|
||||
xhr = $.ajaxSettings.xhr();
|
||||
xhr.upload.onprogress = function (event) {
|
||||
|
||||
if(event.lengthComputable) {
|
||||
|
||||
$("#loaded_n_total").html("Uploaded "+event.loaded+" bytes of "+event.total);
|
||||
var percent = (event.loaded / event.total) * 100;
|
||||
$("#progressBar").val(Math.round(percent));
|
||||
$("#status").html(Math.round(percent)+"% uploaded... please wait");
|
||||
if(options.hasOwnProperty("upload")) {
|
||||
|
||||
xhr=function() {
|
||||
var myXhr = $.ajaxSettings.xhr();
|
||||
if(myXhr.upload){
|
||||
|
||||
var progressbar=options.upload.progressbar;
|
||||
var total_loader=options.upload.total_loader;
|
||||
var status=options.upload.status;
|
||||
|
||||
$(total_loader).html('');
|
||||
$(progressbar).attr('value', 0);
|
||||
$(status).html('');
|
||||
|
||||
myXhr.upload.addEventListener('progress',function (event) {
|
||||
|
||||
$(total_loader).html("Uploaded "+event.loaded+" bytes of "+event.total);
|
||||
var percent = (event.loaded / event.total) * 100;
|
||||
$(progressbar).attr('value', (Math.round(percent)));
|
||||
$(status).html(Math.round(percent)+"% uploaded... please wait");
|
||||
|
||||
}, false);
|
||||
|
||||
myXhr.addEventListener("load", function(event) {
|
||||
|
||||
$(status).html("Done!!");
|
||||
|
||||
}, false);
|
||||
}
|
||||
};
|
||||
return myXhr;
|
||||
}
|
||||
}
|
||||
|
||||
$(this).on('submit', function (e) {
|
||||
|
|
@ -47,50 +64,20 @@
|
|||
|
||||
form=this;
|
||||
|
||||
/*if($(this).attr('enctype'))
|
||||
{*/
|
||||
if(options.separated_data)
|
||||
{
|
||||
|
||||
data[options.separated_data]=new FormData($(this)[0]);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
data=new FormData($(this)[0]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
xhrFields={
|
||||
withCredentials: true
|
||||
};
|
||||
|
||||
/*}
|
||||
if(options.separated_data)
|
||||
{
|
||||
data[options.separated_data]=new FormData($(this)[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if(options.separated_data)
|
||||
{
|
||||
data=new FormData($(this)[0]);
|
||||
}
|
||||
|
||||
|
||||
xhrFields={
|
||||
withCredentials: true
|
||||
};
|
||||
|
||||
data[options.separated_data]=$(this).serializeArray();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
data=$(this).serializeArray().reduce(function(obj, item) {
|
||||
obj[item.name] = item.value;
|
||||
return obj;
|
||||
}, {});
|
||||
|
||||
}
|
||||
|
||||
xhrFields={
|
||||
};
|
||||
|
||||
}*/
|
||||
|
||||
if(!data.hasOwnProperty("csrf_token")) {
|
||||
|
||||
|
|
@ -101,11 +88,8 @@
|
|||
//Hide form and show the time icon
|
||||
|
||||
$(this).find('.error').hide();
|
||||
//$(this).hide();
|
||||
$(options.loading).show();
|
||||
|
||||
/*$(this).find('input').prop("disabled", true);
|
||||
$(this).find('select').prop("disabled", true);*/
|
||||
$(options.loading).show();
|
||||
|
||||
if(options.hasOwnProperty("pre_callback")) {
|
||||
|
||||
|
|
@ -115,20 +99,19 @@
|
|||
|
||||
//Ajax
|
||||
|
||||
$.ajax({
|
||||
ajax_post={
|
||||
type: "POST",
|
||||
url: options.url,
|
||||
data: data,
|
||||
encoding: "UTF-8",
|
||||
xhrFields: xhrFields,
|
||||
cache:false,
|
||||
contentType: false,
|
||||
contentType:false,
|
||||
processData: false,
|
||||
success: function (data) {
|
||||
|
||||
$(options.loading).hide();
|
||||
|
||||
|
||||
|
||||
if(data.error) {
|
||||
|
||||
if(!data.hasOwnProperty("error_csrf"))
|
||||
|
|
@ -148,7 +131,7 @@
|
|||
if(data.hasOwnProperty('error_form'))
|
||||
{
|
||||
for(i in data.error_form) {
|
||||
|
||||
|
||||
$(i).html(data.error_form[i]);
|
||||
$(i).show();
|
||||
|
||||
|
|
@ -186,8 +169,6 @@
|
|||
else
|
||||
{
|
||||
|
||||
|
||||
|
||||
// If ok then post successful task.
|
||||
|
||||
if(options.hasOwnProperty("success")) {
|
||||
|
|
@ -214,7 +195,17 @@
|
|||
|
||||
},
|
||||
dataType: 'json'
|
||||
});
|
||||
};
|
||||
|
||||
//console.log(xhr);
|
||||
|
||||
if(xhr!=false) {
|
||||
|
||||
ajax_post.xhr=xhr;
|
||||
|
||||
}
|
||||
|
||||
$.ajax(ajax_post);
|
||||
|
||||
return false;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue