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 */
|
/* 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)
|
$.fn.sendPost = function (options)
|
||||||
{
|
{
|
||||||
|
|
@ -25,20 +25,37 @@
|
||||||
|
|
||||||
var xhr=false;
|
var xhr=false;
|
||||||
|
|
||||||
if(!options.hasOwnProperty("upload")) {
|
if(options.hasOwnProperty("upload")) {
|
||||||
|
|
||||||
xhr = $.ajaxSettings.xhr();
|
xhr=function() {
|
||||||
xhr.upload.onprogress = function (event) {
|
var myXhr = $.ajaxSettings.xhr();
|
||||||
|
if(myXhr.upload){
|
||||||
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");
|
|
||||||
|
|
||||||
|
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) {
|
$(this).on('submit', function (e) {
|
||||||
|
|
@ -47,50 +64,20 @@
|
||||||
|
|
||||||
form=this;
|
form=this;
|
||||||
|
|
||||||
/*if($(this).attr('enctype'))
|
if(options.separated_data)
|
||||||
{*/
|
{
|
||||||
if(options.separated_data)
|
data[options.separated_data]=new FormData($(this)[0]);
|
||||||
{
|
}
|
||||||
|
|
||||||
data[options.separated_data]=new FormData($(this)[0]);
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
|
|
||||||
data=new FormData($(this)[0]);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
xhrFields={
|
|
||||||
withCredentials: true
|
|
||||||
};
|
|
||||||
|
|
||||||
/*}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
data=new FormData($(this)[0]);
|
||||||
if(options.separated_data)
|
}
|
||||||
{
|
|
||||||
|
|
||||||
|
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")) {
|
if(!data.hasOwnProperty("csrf_token")) {
|
||||||
|
|
||||||
|
|
@ -101,11 +88,8 @@
|
||||||
//Hide form and show the time icon
|
//Hide form and show the time icon
|
||||||
|
|
||||||
$(this).find('.error').hide();
|
$(this).find('.error').hide();
|
||||||
//$(this).hide();
|
|
||||||
$(options.loading).show();
|
|
||||||
|
|
||||||
/*$(this).find('input').prop("disabled", true);
|
$(options.loading).show();
|
||||||
$(this).find('select').prop("disabled", true);*/
|
|
||||||
|
|
||||||
if(options.hasOwnProperty("pre_callback")) {
|
if(options.hasOwnProperty("pre_callback")) {
|
||||||
|
|
||||||
|
|
@ -115,20 +99,19 @@
|
||||||
|
|
||||||
//Ajax
|
//Ajax
|
||||||
|
|
||||||
$.ajax({
|
ajax_post={
|
||||||
type: "POST",
|
type: "POST",
|
||||||
url: options.url,
|
url: options.url,
|
||||||
data: data,
|
data: data,
|
||||||
encoding: "UTF-8",
|
encoding: "UTF-8",
|
||||||
xhrFields: xhrFields,
|
xhrFields: xhrFields,
|
||||||
cache:false,
|
cache:false,
|
||||||
contentType: false,
|
contentType:false,
|
||||||
processData: false,
|
processData: false,
|
||||||
success: function (data) {
|
success: function (data) {
|
||||||
|
|
||||||
$(options.loading).hide();
|
$(options.loading).hide();
|
||||||
|
|
||||||
|
|
||||||
if(data.error) {
|
if(data.error) {
|
||||||
|
|
||||||
if(!data.hasOwnProperty("error_csrf"))
|
if(!data.hasOwnProperty("error_csrf"))
|
||||||
|
|
@ -148,7 +131,7 @@
|
||||||
if(data.hasOwnProperty('error_form'))
|
if(data.hasOwnProperty('error_form'))
|
||||||
{
|
{
|
||||||
for(i in data.error_form) {
|
for(i in data.error_form) {
|
||||||
|
|
||||||
$(i).html(data.error_form[i]);
|
$(i).html(data.error_form[i]);
|
||||||
$(i).show();
|
$(i).show();
|
||||||
|
|
||||||
|
|
@ -186,8 +169,6 @@
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// If ok then post successful task.
|
// If ok then post successful task.
|
||||||
|
|
||||||
if(options.hasOwnProperty("success")) {
|
if(options.hasOwnProperty("success")) {
|
||||||
|
|
@ -214,7 +195,17 @@
|
||||||
|
|
||||||
},
|
},
|
||||||
dataType: 'json'
|
dataType: 'json'
|
||||||
});
|
};
|
||||||
|
|
||||||
|
//console.log(xhr);
|
||||||
|
|
||||||
|
if(xhr!=false) {
|
||||||
|
|
||||||
|
ajax_post.xhr=xhr;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$.ajax(ajax_post);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue