Skip to content

Instantly share code, notes, and snippets.

@Verurteilt
Last active December 19, 2015 19:49
Show Gist options
  • Save Verurteilt/6009292 to your computer and use it in GitHub Desktop.
Save Verurteilt/6009292 to your computer and use it in GitHub Desktop.
FIle for StackOverflow problem
$(document).ajaxSend(function(event, xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function sameOrigin(url) {
// url could be relative or scheme relative or absolute
var host = document.location.host; // host + port
var protocol = document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin;
// Allow absolute or scheme relative URLs to same origin
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
// or any other URL that isn't scheme relative or absolute i.e relative.
!(/^(\/\/|http:|https:).*/.test(url));
}
function safeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
});
//////////////////////////////////////////////////////////////////////////////////////////////////
//
/**
* Emulate FormData for some browsers
* MIT License
* (c) 2010 François de Metz
*/
(function(w) {
if (w.FormData)
return;
function FormData() {
this.fake = true;
this.boundary = "--------FormData" + Math.random();
this._fields = [];
}
FormData.prototype.append = function(key, value) {
this._fields.push([key, value]);
};
FormData.prototype.toString = function() {
var boundary = this.boundary;
var body = "";
this._fields.forEach(function(field) {
body += "--" + boundary + "\r\n";
// file upload
if (field[1].name) {
var file = field[1];
body += "Content-Disposition: form-data; name=\""+ field[0] +"\"; filename=\""+ file.name +"\"\r\n";
body += "Content-Type: "+ file.type +"\r\n\r\n";
body += file.getAsBinary() + "\r\n";
} else {
body += "Content-Disposition: form-data; name=\""+ field[0] +"\";\r\n\r\n";
body += field[1] + "\r\n";
}
});
body += "--" + boundary +"--";
return body;
};
w.FormData = FormData;
})(window);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function es_fancy(contenido){
$.fancybox(contenido);
dispararTicket();
}
function post_thank(url, data){
$.ajax({
url:url,
data: data,
type: "POST",
success: function(data){
es_fancy(data);
},
beforeSend : function(data){
$.each($('.errorlist'), function(){
$('.errorlist').html('');
});
$('.errores').html('');
},
error: function(data){
var datas = $.parseJSON(data.responseText);
if (datas.serv_error){
$('.errores').append('<p>'+datas.serv_error+'</p>');
}
},
processData: false,
contentType: false
});
}
function InsDB () {
$('.errores').empty();
var promotor = document.getElementById('div_formulario').getAttribute('data-promotor');
var promocion = document.getElementById('div_formulario').getAttribute('data-promocion');
var landing = document.getElementById('div_formulario').getAttribute('data-landing');
var is_prueba = document.getElementById('div_formulario').getAttribute('data-prueba');
var url = '/landing/funciona_form/';
$('form').ajaxSubmit({
type: "POST",
url: url,
data: {'clave_promotor': promotor, 'clave_promocion': promocion, 'prueba': is_prueba, 'clave_landing': landing},
success: function(data){
if (data.response.thank_you_page && data.response.tipo == "html"){
window.location.assign(data.response.thank_you_page);
}else{
post_thank(data.response.thank_you_page, new FormData($('#form-datos').get(0)));
// es_fancy(data.response.contenido);
}
var form = document.getElementById('form-datos');
form.reset();
},
beforeSubmit : function(data){
$.each($('.errorlist'), function(){
$('.errorlist').html('');
});
$('.errores').html('');
},
error: function(data){
var datas = $.parseJSON(data.responseText);
if (datas.serv_error){
$('.errores').append('<p>'+datas.serv_error+'</p>');
}else{
$('#div_formulario').append("<div id='form_error'></div>");
$.each(datas.errores, function(index, value){
$('#'+index+'_error').html('<li>'+value+'</li>');
});
window.location.assign("#form-datos");
}
},
processData: false,
contentType: false
});
}
function Archivotxt () {
$('.errores').empty();
var promotor = document.getElementById('div_formulario').getAttribute('data-promotor');
var promocion = document.getElementById('div_formulario').getAttribute('data-promocion');
var landing = document.getElementById('div_formulario').getAttribute('data-landing');
var is_prueba = document.getElementById('div_formulario').getAttribute('data-prueba');
var url = '/landing/funciona_form/';
$('form').ajaxSubmit({
type: "POST",
url: url,
data: {'clave_promotor': promotor, 'clave_promocion': promocion, 'prueba': is_prueba, 'clave_landing': landing},
success: function(data){
if (data.response.thank_you_page && data.response.tipo == "html"){
window.location.assign(data.response.thank_you_page);
}else{
post_thank(data.response.thank_you_page, new FormData($('#form-datos').get(0)));
// es_fancy(data.response.contenido);
}
var form = document.getElementById('form-datos');
form.reset();
},
beforeSubmit : function(data){
$.each($('.errorlist'), function(){
$('.errorlist').html('');
});
$('.errores').html('');
},
error: function(data){
var datas = $.parseJSON(data.responseText);
if (datas.serv_error){
$('.errores').append('<p>'+datas.serv_error+'</p>');
}else{
$('#div_formulario').append("<div id='form_error'></div>");
$.each(datas.errores, function(index, value){
$('#'+index+'_error').html('<li>'+value+'</li>');
});
window.location.assign("#form-datos");
}
},
processData: false,
contentType: false
});
}
function cargaForm () {
var dataform = document.getElementById('div_formulario').getAttribute('data-form');
var dataprueba = document.getElementById('div_formulario').getAttribute('data-prueba');
var url = '/landing/traer_form/?name_form='+dataform+"&data_prueba=" +dataprueba;
$.ajax({
url:url,
dataType: 'html',
type: "GET",
beforeSend: function(xhr){
$.fancybox.showActivity();
},
success: function(data){
$.fancybox.hideActivity();
var div = document.getElementById('div_formulario');
div.innerHTML=data;
try{
codpos_autocomplete_json();
}catch(error){
}
try{
date_p();
}catch(error){
}
}
});
}
$(document).ready(function(){
setTimeout('cargaForm();', 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment